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)); }