Java中的Lambda匿名函數後續
- 2021 年 2 月 17 日
- 筆記
函數式編程(函數式介面):一個介面只包含一個方法實現
public interface Lambda{
void method();
}
// 調用
Lambda lambda = new Lambda(){
};
Lambda lambda1 = () - >{
};
Lambda lambda2 = () ->xxx;
Function介面,一個參數對應一個返回值
Supplier 一個輸出
Consumer一個輸入
BiFuction 兩個輸入一個輸出
BiConsumer 兩個輸入
方法引用
-
靜態方法引用
類名::staticMethod, lambda:(args) ——>類名.staticMethod(args);
-
實例方法引用
實例::實例方法,lambda:(args)——>實例.實例方法(args);
-
對象方法引用
類名::實例方法,lambda:(實例, args) —> 實例.實例方法(args); 第一個參數類型必須為實例方法對應的類
-
構造方法引用
類名::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));
}