1.Lambda的设计者们为了让现有的功能与Lambda表达式良好兼容,考虑了很多方法,于是产生了函数接口这个概念。函数接口指的是只有一个函数的接口,这样的接口可以隐式转换为Lambda表达式。
2. java.lang.Runnable和java.util.concurrent.Callable是函数式接口的最佳例子。
3.在实践中,函数式接口非常脆弱:只要某个开发者在该接口中添加一个函数,则该接口就不再是函数式接口进而导致编译失败。为了克服这种代码层面的脆弱性,并显式说明某个接口是函数式接口,Java 8 提供了一个特殊的注解@FunctionalInterface(Java 库中的所有相关接口都已经带有这个注解了),举个简单的函数式接口的定义:
@FunctionalInterface
public interface FunctionInterface {
void method();
}
4.不过有一点需要注意,默认方法和静态方法不会破坏函数式接口的定义,因此如下的代码是合法的。
@FunctionalInterface
public interface FunctionInterface {
void method();
/**
* 默认方法
*/
default int add(int x,int y) {
return x+y;
}
/**
* 静态方法
*/
static void call() {
System.out.println("static method call()");
}
}
5.函数式接口实例
Predicate <T> 接口是一个函数式接口,它接受一个输入参数 T,返回一个布尔值结果。
该接口包含多种默认方法来将Predicate组合成其他复杂的逻辑(比如:与,或,非)。
该接口用于测试对象是 true 或 false。
我们可以通过以下实例(Java8Test.java)来了解函数式接口 Predicate <T> 的使用:
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class Java8Test {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
// Predicate<Integer> predicate = n -> true
// n 是一个参数传递到 Predicate 接口的 test 方法
// n 如果存在则 test 方法返回 true
System.out.println("输出所有数据:");
// 传递参数 n
eval(list, n->true);
System.out.println("===========================");
// Predicate<Integer> predicate1 = n -> n%2 == 0
// n 是一个参数传递到 Predicate 接口的 test 方法
// 如果 n%2 为 0 test 方法返回 true
System.out.println("输出所有偶数:");
eval(list, n-> n%2 == 0 );
System.out.println("===========================");
// Predicate<Integer> predicate2 = n -> n > 3
// n 是一个参数传递到 Predicate 接口的 test 方法
// 如果 n 大于 3 test 方法返回 true
System.out.println("输出大于 3 的所有数字:");
eval(list, n-> n > 3 );
}
public static int eval(List<Integer> list, Predicate<Integer> predicate){
int count = 0;
for(Integer n: list) {
if(predicate.test(n)) {
System.out.println(n + " ");
count += 1;
}
}
return count;
}
}
输出结果如下:
输出所有数据:
1
2
3
4
5
===========================
输出所有偶数:
2
4
===========================
输出大于 3 的所有数字:
4
5
6.Function接口示例
Function<Integer, Integer> name = e -> e * 2;
Function<Integer, Integer> square = e -> e * e;
int value = name.andThen(square).apply(3);
System.out.println("3*2=6 6*6=36");
System.out.println("先执行name,再使用name返回的结果执行square=" + value);
int value2 = name.compose(square).apply(3);
System.out.println("\n3*3=9 9*2=18");
System.out.println("先执行square,再使用square返回的结果执行name=" + value2);
System.out.println("直接执行结果"+name.apply(5));
/**
3*2=6 6*6=36
先执行name,再使用name返回的结果执行square=36
3*3=9 9*2=18
先执行square,再使用square返回的结果执行name=18
直接执行结果10
**/
7.JDK 1.8 新增加的函数接口:
- java.util.function 它包含了很多类,用来支持 Java的 函数式编程,该包中的函数式接口有:
序号 | 接口 & 描述 |
---|---|
1 | BiConsumer<T,U> 代表了一个接受两个输入参数的操作,并且不返回任何结果 |
2 | BiFunction<T,U,R> 代表了一个接受两个输入参数的方法,并且返回一个结果 |
3 | BinaryOperator<T> 代表了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果 |
4 | BiPredicate<T,U> 代表了一个两个参数的boolean值方法 |
5 | BooleanSupplier 代表了boolean值结果的提供方 |
6 | Consumer<T> 代表了接受一个输入参数并且无返回的操作 |
7 | DoubleBinaryOperator 代表了作用于两个double值操作符的操作,并且返回了一个double值的结果。 |
8 | DoubleConsumer 代表一个接受double值参数的操作,并且不返回结果。 |
9 | DoubleFunction<R> 代表接受一个double值参数的方法,并且返回结果 |
10 | DoublePredicate 代表一个拥有double值参数的boolean值方法 |
11 | DoubleSupplier 代表一个double值结构的提供方 |
12 | DoubleToIntFunction 接受一个double类型输入,返回一个int类型结果。 |
13 | DoubleToLongFunction 接受一个double类型输入,返回一个long类型结果 |
14 | DoubleUnaryOperator 接受一个参数同为类型double,返回值类型也为double 。 |
15 | Function<T,R> 接受一个输入参数,返回一个结果。 |
16 | IntBinaryOperator 接受两个参数同为类型int,返回值类型也为int 。 |
17 | IntConsumer 接受一个int类型的输入参数,无返回值 。 |
18 | IntFunction<R> 接受一个int类型输入参数,返回一个结果 。 |
19 | IntPredicate :接受一个int输入参数,返回一个布尔值的结果。 |
20 | IntSupplier 无参数,返回一个int类型结果。 |
21 | IntToDoubleFunction 接受一个int类型输入,返回一个double类型结果 。 |
22 | IntToLongFunction 接受一个int类型输入,返回一个long类型结果。 |
23 | IntUnaryOperator 接受一个参数同为类型int,返回值类型也为int 。 |
24 | LongBinaryOperator 接受两个参数同为类型long,返回值类型也为long。 |
25 | LongConsumer 接受一个long类型的输入参数,无返回值。 |
26 | LongFunction<R> 接受一个long类型输入参数,返回一个结果。 |
27 | LongPredicate R接受一个long输入参数,返回一个布尔值类型结果。 |
28 | LongSupplier 无参数,返回一个结果long类型的值。 |
29 | LongToDoubleFunction 接受一个long类型输入,返回一个double类型结果。 |
30 | LongToIntFunction 接受一个long类型输入,返回一个int类型结果。 |
31 | LongUnaryOperator 接受一个参数同为类型long,返回值类型也为long。 |
32 | ObjDoubleConsumer<T> 接受一个object类型和一个double类型的输入参数,无返回值。 |
33 | ObjIntConsumer<T> 接受一个object类型和一个int类型的输入参数,无返回值。 |
34 | ObjLongConsumer<T> 接受一个object类型和一个long类型的输入参数,无返回值。 |
35 | Predicate<T> 接受一个输入参数,返回一个布尔值结果。 |
36 | Supplier<T> 无参数,返回一个结果。 |
37 | ToDoubleBiFunction<T,U> 接受两个输入参数,返回一个double类型结果 |
38 | ToDoubleFunction<T> 接受一个输入参数,返回一个double类型结果 |
39 | ToIntBiFunction<T,U> 接受两个输入参数,返回一个int类型结果。 |
40 | ToIntFunction<T> 接受一个输入参数,返回一个int类型结果。 |
41 | ToLongBiFunction<T,U> 接受两个输入参数,返回一个long类型结果。 |
42 | ToLongFunction<T> 接受一个输入参数,返回一个long类型结果。 |
43 | UnaryOperator<T> 接受一个参数为类型T,返回值类型也为T。 |