四大函数式接口(消费型接口(Consumer)和供给型接口(Supplier))
第一步:消费型接口(Consumer):只有输入参数,返回值(接口的方法是void和参数)

测试:

Lambda表达式简化:


第二步:供给型接口(Supplier):没有参数只有返回值(注意是看我们接口的方法没有参数)


Lambda简化


作用:简化编程模型
- package org.example.threadpoolexecutor;
-
-
-
- import java.util.function.Consumer;
-
-
-
- /**
- * 四大函数型接口(有这个注解@FunctionalInterface)消费型接口。有输入参数,没有输出
- */
-
- public class TestConsumer {
-
- public static void main(String[] args) {
-
- // Consumer
consumer = new Consumer() { -
- // @Override
-
- // public void accept(String str) {
-
- // System.out.println(str);
-
- // }
-
- // };
-
- Consumer
consumer=(str)->{ -
- System.out.println(str);
-
- };
-
- consumer.accept("ssss");
-
-
-
- }
-
-
-
- }
-
- package org.example.threadpoolexecutor;
-
-
-
- import java.util.function.Supplier;
-
-
-
- /**
- * 四大函数型接口(有这个注解@FunctionalInterface)供给型接口。没有输入参数,有返回值
- */
-
- public class TestSupplier {
-
- public static void main(String[] args) {
-
- // Supplier
supplier = new Supplier() { -
- // @Override
-
- // public String get() {
-
- // return "66666";
-
- // }
-
- // };
-
- Supplier
supplier=()->{ -
- return "6666";
-
- };
-
- System.out.println(supplier.get());
-
- }
-
- }
-
-
-
-
-