Function<T, R>
T:入參類型,R:出參類型
調用方法:R apply(T t);
定義函數示例:Function<Integer, Integer> func = p -> p * 10; // 輸出入參的10倍
調用函數示例:func.apply(10); // 結果100
Consumer<T>
T:入參類型;沒有出參
調用方法:void accept(T t);
定義函數示例:Consumer<String> consumer= p -> System.out.println(p); // 因爲沒有出參,常用於打印、發送短信等消費動作
調用函數示例:consumer.accept("18800008888");
Supplier<T>
T:出參類型;沒有入參
調用方法:T get();
定義函數示例:Supplier<Integer> supplier= () -> 100; // 常用於業務“有條件運行”時,符合條件再調用獲取結果的應用場景;運行結果須提前定義,但不運行。
調用函數示例:supplier.get();
Predicate<T>
T:入參類型;出參類型是Boolean
調用方法:boolean test(T t);
定義函數示例:Predicate<Integer> predicate = p -> p % 2 == 0; // 判斷是否、是不是偶數
調用函數示例:predicate.test(100); // 運行結果true