public interface Lambda{ void method(); } // 调用 Lambda lambda = new Lambda(){ }; Lambda lambda1 = () - >{ }; Lambda lambda2 = () ->xxx;
静态方法引用java
类名::staticMethod, lambda:(args) ——>类名.staticMethod(args);编程
实例方法引用app
实例::实例方法,lambda:(args)——>实例.实例方法(args);函数式编程
对象方法引用函数
类名::实例方法,lambda:(实例, args) ---> 实例.实例方法(args); 第一个参数类型必须为实例方法对应的类学习
构造方法引用code
类名::new,lambda:(args) ---->new 类名(args);对象
package Lambda; import java.util.ArrayList; import java.util.List; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Supplier; public class Test { public Test(){ System.out.println("create"); } public static void main(String[] args) { // final Lambda lambda = Test::method1; // lambda.method(1); // List<Lambda> lambdas = new ArrayList<Lambda>(); // for (int i = 0; i < 10; i++) { // lambdas.add(Test::method1); // } // // for (int i = 0; i < 10; i++) { // lambdas.get(i).method(i); // } // Lambda lambda = new Test()::method; // lambda.method(); // Lambda lambda = ()->new Test().method(); // lambda.method(); // Lambda lambda = Test::new; // lambda.method(); // Lambda lambda = ()->new Test(); // lambda.method(); // 静态方法 Lambda lambda = Test::method1; Lambda lambda4 = () -> Test.method1(); // 实例方法 Lambda lambda1 = new Test()::method; Lambda lambda5 = ()-> new Test().method(); // 对象方法,要求前面接口的泛型类型须要实例方法的类类型一直 // Lambda lambda2 = Test::method; Consumer<Test> consumer0 = (s) -> new Test().method1("hhhh"); Consumer<Test> consumer =Test::method; // 构造方法 Lambda lambda3 = Test::new; Lambda lambda7 = ()->new Test(); } void method(){ System.out.println("method"); } static void method1(String s){ System.out.println(s); } }
用一个集合存放多个方法的引用,用的时候再调用接口
List<Lambda> lambdas = new ArrayList<Lambda>(); for (int i = 0; i < 10; i++) { lambdas.add(Test::method1); } for (int i = 0; i < 10; i++) { lambdas.get(i).method(i); }
Consumer<Integer> consumer = (i) -> System.out.println(i); consumer.accept(10); Supplier<Integer> supplier = () ->100; System.out.println(supplier.get()); Function<String, Integer> function = (str)->str.length(); System.out.println(function.apply("嗨咯")); BiFunction<String, String, Integer> biFunction = (str1, str2)->str1.length()+str2.length(); System.out.println(biFunction.apply("嗨喽", "学习Java"));
// 定义 static void method4(int i, Consumer<Integer> consumer){ consumer.accept(i); } static int method5(Supplier<Integer> supplier){ return supplier.get(); } static int method6(String s, Function<String, Integer> function){ return function.apply(s); } static int method7(String s1, String s2, BiFunction<String, String, Integer> biFunction){ return biFunction.apply(s1, s2); } // 调用 Consumer<Integer> consumer = (i) -> System.out.println(i); consumer.accept(10); Supplier<Integer> supplier = () ->100; System.out.println(supplier.get()); Function<String, Integer> function = (str)->str.length(); System.out.println(function.apply("嗨咯")); BiFunction<String, String, Integer> biFunction = (str1, str2)->str1.length()+str2.length(); System.out.println(biFunction.apply("嗨喽", "学习Java")); for (int i = 0; i < 20; i++) { method4(i,consumer); System.out.println(method5(supplier)); System.out.println(method6(String.valueOf(i), function)); System.out.println(method7(String.valueOf(i), String.valueOf(i), biFunction)); }