赞
踩
目录
先看lambda表达式是怎么写的,demo:
- public class Demo01Function {
- public static void main(String[] args) {
- fun1((arr) -> {
- int sum = 0;
- for (int i : arr) {
- sum +=i;
- }
- return sum;
- });
- }
-
- public static void fun1(Operator operator){
- int[] arr = {1,2,3,4};
- int sum = operator.getSum(arr);
- System.out.println("sum=" + sum);
- }
- }

- /**
- * 函数式接口
- */
- @FunctionalInterface
- interface Operator{
- int getSum(int[] arr);
- }
发现,
1.我们要自己定义接口Operator,然而实际上lambda表达式中,我们并不关心函数式接口的接口名和
抽象方法名
2.lambda表达式只关心参数列表和返回值类型
因此,
jdk8中给我们提供了很多常用的函数式接口。
在JDK中帮我们提供的由函数式接口,主要在java.util,function包中
无参有返回值的接口
- @FunctionalInterface
- public interface Supplier<T> {
-
- /**
- * Gets a result.
- *
- * @return a result
- */
- T get();
- }
demo,SupplierTest:
- public class SupplierTest {
- public static void main(String[] args) {
- fun1(() -> {
- int arr[] = {22,44,32,87,22,67};
- //计算出数组中的最大值
- Arrays.sort(arr);
- return arr[arr.length - 1];
- });
- }
-
- private static void fun1(Supplier<Integer> supplier){
- //get()是一个无参有返回值的抽象方法
- Integer max = supplier.get();
- System.out.println("max=" + max);
- }
- }

运行输出:max=87
有参无返回值的接口,前面介绍的Supplier接口是生产数据的,而Consumer是消费数据的
jdk中源码:
- @FunctionalInterface
- public interface Consumer<T> {
-
- /**
- * Performs this operation on the given argument.
- *
- * @param t the input argument
- */
- void accept(T t);
- }
demo ConsumerTest:
- public class ConsumerTest {
-
- public static void main(String[] args) {
- fun1(msg -> {
- System.out.println(msg + "--转换为大写-->" + msg.toUpperCase());
- });
- }
-
- private static void fun1(Consumer<String> consumer){
- consumer.accept("HelloWorld");
- }
- }
运行输出:HelloWorld--转换为大写-->HELLOWORLD
默认方法 andThen:
如果一个方法的参数和返回值全部是Consumer类型,那么就可以实现效果,消费一个数据的时候,首先做一个操作然后再做一个操作实现组合,而这个方法就是Consumer接口中的default方法andThen方法
- public class ConsumerAndThenTest {
- public static void main(String[] args) {
- test(msg1 -> {
- System.out.println(msg1 + "--转换为大写-->" + msg1.toUpperCase());
- },msg2 -> {
- System.out.println(msg2 + "--转换为大写-->" + msg2.toLowerCase());
- });
- }
-
- private static void test(Consumer<String> c1,Consumer<String> c2){
- c1.andThen(c2).accept("HelloWorld");
- }
- }
运行输出:HelloWorld--转换为大写-->HELLOWORLD
HelloWorld--转换为大写-->helloworld
有参有返回值的函数式接口,
Function接口时根据一中类型的数据得到另一种类型的数据,前置成为前置条件,后者成为后置条件。
- @FunctionalInterface
- public interface Function<T, R> {
-
- /**
- * Applies this function to the given argument.
- *
- * @param t the function argument
- * @return the function result
- */
- R apply(T t);
- }
- public class FunctionTest {
- public static void main(String[] args) {
- test(msg -> {
- int i = Integer.parseInt(msg);
- return i;
- });
- }
-
- //将String类型转换为Integer类型
- private static void test(Function<String,Integer> function){
- Integer apply = function.apply("666");
- System.out.println("apply=" + apply);
- }
- }

默认方法 andThen,也是用来进行组合操作的
- public class FunctionAndThenTest {
- public static void main(String[] args) {
- test(msg1 -> {
- return Integer.parseInt(msg1);
- },msg2 -> {
- return msg2 * 10;
- });
- }
-
- static void test(Function<String,Integer> f1, Function<Integer,Integer> f2){
- Integer apply = f1.andThen(f2).apply("666");
- System.out.println("apply:" + apply);
- }
- }
有参且返回值是布尔类型
- @FunctionalInterface
- public interface Predicate<T> {
-
- /**
- * Evaluates this predicate on the given argument.
- *
- * @param t the input argument
- * @return {@code true} if the input argument matches the predicate,
- * otherwise {@code false}
- */
- boolean test(T t);
使用:
- public class PredicateTest {
- public static void main(String[] args) {
- test((String msg) -> {
- return msg.length() > 3;
- },"hello");
- }
-
- private static void test(Predicate<String> predicate,String msg){
- boolean test = predicate.test(msg);
- System.out.println("b:" + test);
- }
- }
输出:b:true
再Predicate中提供了默认的逻辑关系操作:
- public class PredicateOtherMethodTest {
- public static void main(String[] args) {
- test(msg1 -> {
- return msg1.contains("H");
- },msg2 -> {
- return msg2.contains("W");
- });
- }
- private static void test(Predicate<String> p1,Predicate<String> p2){
- //与
- boolean hello1 = p1.and(p2).test("Hello");
- //或
- boolean hello2 = p1.or(p2).test("Hello");
- //非
- boolean hello3 = p1.negate().test("Hello");
- System.out.println("hello1:" + hello1);
- System.out.println("hello2:" + hello2);
- System.out.println("hello3:" + hello3);
- }
- }

输出: hello1:false hello2:true hello3:false
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。