// Function<T, R> -T作为输入,返回的R作为输出
Function<String,String> function = (x) -> {System.out.print(x+": ");return "Function";};
System.out.println(function.apply("hello world"));
//Predicate<T> -T作为输入,返回的boolean值作为输出
Predicate<String> pre = (x) ->{System.out.print(x);return false;};
System.out.println(": "+pre.test("hello World"));
//Consumer<T> - T作为输入,执行某种动作但没有返回值
Consumer<String> con = (x) -> {System.out.println(x);};
con.accept("hello world");
//Supplier<T> - 没有任何输入,返回T
Supplier<String> supp = () -> {return "Supplier";};
System.out.println(supp.get());
//BinaryOperator<T> -两个T作为输入,返回一个T作为输出,对于“reduce”操作很有用
BinaryOperator<String> bina = (x,y) ->{System.out.print(x+" "+y);return "BinaryOperator";};
System.out.println(" "+bina.apply("hello ","world"));
java8中几个函数式接口的小例子
最新推荐文章于 2024-07-30 23:47:33 发布
本文深入探讨了 Java 8 中的函数式接口,包括 Function、Predicate、Consumer、Supplier 和 BinaryOperator 的使用方法及应用场景,为读者提供了丰富的代码示例。
1334

被折叠的 条评论
为什么被折叠?



