JDK 8 的函数式接口主要在java.util.function
包下,大概有四十几个。如下图:
本文我们只介绍比较重要的几个函数式接口,其他的就不再说明了,如有需要,可以自己根据官方文档研究一下。
什么是函数式接口
函数式接口
(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。- 你可以通过 Lambda 表达式来创建该接口的对象。(若 Lambda 表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进行声明)。
- 我们可以在任意函数式接口上使用
@FunctionalInterface
注解,这样做可以检查它是否是一个函数式接口,同时javadoc
也会包含一条声明,说明这个接口是一个函数式接口。
自定义函数式接口
函数式接口
// 函数式接口中使用泛型
@FunctionalInterface
public interface MyFunc<T> {
public T getValue(T t);
}
测试用例
package 函数式接口.自定义;
import org.junit.Test;
public class MyFuncTest {
@Test
public void testGetValue() {
// 作为参数传递 Lambda 表达式
String newStr = toUpperString(str -> str.toUpperCase(), "abcdef");
System.out.println(newStr);
}
private String toUpperString(MyFunc<String> mf, String str) {
return mf.getValue(str);
}
}
作为参数传递 Lambda 将 表达式:为了将 Lambda 表达式作为参数传递,接收 Lambda 表达式的参数类型必须是与该 Lambda 表达式兼容的函数式接口的类型。
运行结果
ABCDEF
Java 内置四大核心函数式接口
下面我们来介绍 Java 内置的四大核心函数式接口。
JDK 8 虽然有四十几个函数式接口,但都是根据不同用途对这四个接口进行的扩展。
函数式接口 | 参数类型 | 返回类型 | 用途 |
---|---|---|---|
Consumer<T> :消费型接口 |
T | void | 对类型为T的对象应用操作。 包含方法: void accept(T t) |
Supplier<T> :供给型接口 |
无 | T | 返回类型为T的对象。 包含方法: T get(); |
Function<T, R> :函数型接口 |
T | R | 对类型为T的对象应用操作,并返回结果。结果是R类型的对象。 包含方法: R apply(T t); |
Predicate<T> :断定型接口 |
T | boolean | 确定类型为T的对象是否满足某约束,并返回boolean 值。 包含方法: boolean test(T t); |
Consumer<T>
:消费型接口
// Consumer<T>:消费型接口
@Test
public void testConsumer() {
// 匿名函数方式
Consumer<Double> consumer = (m) -> System.out.println("这是多少钱:" + m);
consumer.accept(1000D);
// 将 Lambda 表达式作为参数进行传递
happy(10000, (m) -> System.out.println("这是多少钱:" + m));
}
public void happy(double money, Consumer<Double> con) {
con.accept(money);
}
运行结果:
这是多少钱:1000.0
这是多少钱:10000.0
Supplier<T>
:供给型接口
// Supplier<T>:供给型接口
@Test
public void testSupplier() {
List<Integer> numList = getNumList(10, () -> (int) (Math.random() * 100));
for (Integer num : numList) {
System.out.printf("%4d", num);
}
}
// 需求:产生指定个数的整数,并放入集合中
public List<Integer> getNumList(int num, Supplier<Integer> sup) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
Integer n = sup.get();
list.add(n);
}
return list;
}
运行结果:
21 85 30 16 4 39 35 69 4 38
Function<T, R>
:函数型接口
// Function<T, R>:函数型接口
@Test
public void testFunction() {
String newStr = strHandler("\t\t\t 挖坑埋你 ", (str) -> str.trim());
System.out.println(newStr);
String subStr = strHandler("挖坑埋你", (str) -> str.substring(1, 2));
System.out.println(subStr);
}
// 需求:用于处理字符串
public String strHandler(String str, Function<String, String> fun) {
return fun.apply(str);
}
运行结果:
挖坑埋你
坑
Predicate<T>
:断言型接口
// Predicate<T>:断言型接口
@Test
public void testPredicate() {
List<String> list = Arrays.asList("Hello", "waKengMaiNi", "Lambda", "www", "ok");
List<String> strList = filterStr(list, (s) -> s.length() > 3);
for (String str : strList) {
System.out.println(str);
}
}
// 需求:将满足条件的字符串,放入集合中
public List<String> filterStr(List<String> list, Predicate<String> pre) {
List<String> strList = new ArrayList<>();
for (String str : list) {
if (pre.test(str)) {
strList.add(str);
}
}
return strList;
}
运行结果:
Hello
waKengMaiNi
Lambda
其他接口
函数式接口 | 参数类型 | 返回类型 | 用途 |
---|---|---|---|
BiFunction<T, U, R> |
T, U | R | 对类型为 T, U 参数应用操作,返回 R 类型的结果。 包含方法为 R apply(T t, U u); |
UnaryOperator <T> (Function 子接口) |
T | T | 对类型为 T 的对象进行一元运算(参数和返回值都为 T 类型),并返回 T 类型的结果。 包含方法为 T apply(T t); |
BinaryOperator<T> (BiFunction 子接口) |
T, T | T | 对类型为T的对象进行二元运算,并返回T类型的结果。 包含方法为 T apply(T t1, T t2); |
BiConsumer<T, U> |
T, U | void | 对类型为T, U 参数应用操作。 包含方法为 void accept(T t, U u) |
ToIntFunction<T> ToLongFunction<T> ToDoubleFunction<T> |
T | int long double |
分 别 计 算 int 、 long 、double值的函数 |
IntFunction<R> LongFunction<R> DoubleFunction<R> |
int long double |
R | 参数分别为int、long、double 类型的函数 |