函数式接口
什么是函数式接口
- 只包含一个抽象方法的接口,称为函数式接口。
- 你可以通过Lambda表达式来创建该接口的对象。(若Lambda表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进行声明)。
- 我们可以在任意函数式接口上使用**@FunctionalInterface**注解,这样做可以检查它是否是一个函数式接口,同时javadoc 也会包含一条声明,说明这个接口是一个函数式接口。
Java 内置四大核心函数式接口
| 函数式接口 | 参数类型 | 返回类型 | 用途 |
|---|---|---|---|
| Consumer 消费型接口 | T | void | 对类型为T的对象应用操 作,包含方法: void accept(T t) |
| Supplier 供给型接口 | 无 | T | 返回类型为T的对象,包 含方法:T get(); |
| Function 函数型接口 | T | R | 对类型为T的对象应用操 作,并返回结果。结果 是R类型的对象。包含方 法:R apply(T t); |
| Predicate 断定型接口 | T | boolean | 确定类型为T的对象是否 满足某约束,并返回 boolean 值。包含方法: boolean test(T t); |
其他接口
| 函数式接口 | 参数类型 | 返回类型 | 用途 |
|---|---|---|---|
| BiFunction<T, U, R> | T, U | R | 对类型为 T, U 参数应用 操作,返回 R 类型的结 果。包含方法为 R apply(T t, U u); |
| UnaryOperator (Function子接口) | T | T | 对类型为T的对象进行一 元运算,并返回T类型的 结果。包含方法为 T apply(T t); |
| BinaryOperator (BiFunction 子接口) | T, T | void | 对类型为T的对象进行二 元运算,并返回T类型的 结果。包含方法为 T apply(T t1, T t2); |
| BiConsumer<T, U> | T, U | void | 对类型为T, U 参数应用 操作。包含方法为 void accept(T t, U u) |
| ToIntFunction < T > | T | int | 计 算 int 值的函数 |
| ToLongFunction< T > | T | long | 计 算 long 值的函数 |
| ToDoubleFunction< T > | T | double | 计 算 double值的函数 |
| IntFunction< R > | int | R | 参数为int类型的函数 |
| LongFunction< R > | long | R | 参数为long 类型的函数 |
| DoubleFunction< R > | double | R | 参数为 double 类型的函数 |
方法引用
方法引用
方法引用:若 Lambda 体中的功能,已经有方法提供了实现,可以使用方法引用(可以将方法引用理解为 Lambda 表达式的另外一种表现形式)
-
对象 :: 实例方法名
-
类名 :: 静态方法名
-
类名 :: 实例方法名
注意:
① 方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致!
② 若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName
//类名 :: 实例方法名
@Test
public void test5(){
BiPredicate<String, String> bp = (x, y) -> x.equals(y);
System.out.println(bp.test("abcde", "abcde"));
System.out.println("-----------------------------------------");
BiPredicate<String, String> bp2 = String::equals;
System.out.println(bp2.test("abc", "abc"));
System.out.println("-----------------------------------------");
Function<Employee, String> fun = (e) -> e.show();
System.out.println(fun.apply(new Employee()));
System.out.println("-----------------------------------------");
Function<Employee, String> fun2 = Employee::show;
System.out.println(fun2.apply(new Employee()));
}
//类名 :: 静态方法名
@Test
public void test4(){
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
System.out.println("-------------------------------------");
Comparator<Integer> com2 = Integer::compare;
}
@Test
public void test3(){
BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);
System.out.println(fun.apply(1.5, 22.2));
System.out.println("--------------------------------------------------");
BiFunction<Double, Double, Double> fun2 = Math::max;
System.out.println(fun2.apply(1.2, 1.5));
}
//对象的引用 :: 实例方法名
@Test
public void test2(){
Employee emp = new Employee(101, "张三", 18, 9999.99);
Supplier<String> sup = () -> emp.getName();
System.out.println(sup.get());
System.out.println("----------------------------------");
Supplier<String> sup2 = emp::getName;
System.out.println(sup2.get());
}
@Test
public void test1(){
PrintStream ps = System.out;
Consumer<String> con = (str) -> ps.println(str);
con.accept("Hello World!");
System.out.println("--------------------------------");
Consumer<String> con2 = ps::println;
con2.accept("Hello Java8!");
Consumer<String> con3 = System.out::println;
}
构造器引用
构造器引用 :构造器的参数列表,需要与函数式接口中参数列表保持一致!
类名 :: new
//构造器引用
@Test
public void test7(){
Function<String, Employee> fun = Employee::new;
BiFunction<String, Integer, Employee> fun2 = Employee::new;
}
@Test
public void test6(){
Supplier<Employee> sup = () -> new Employee();
System.out.println(sup.get());
System.out.println("------------------------------------");
Supplier<Employee> sup2 = Employee::new;
System.out.println(sup2.get());
}
数组引用
类型[] :: new
//数组引用
@Test
public void test8(){
Function<Integer, String[]> fun = (args) -> new String[args];
String[] strs = fun.apply(10);
System.out.println(strs.length);
System.out.println("--------------------------");
Function<Integer, Employee[]> fun2 = Employee[] :: new;
Employee[] emps = fun2.apply(20);
System.out.println(emps.length);
}