1.常用函數是接口:
(1)Function<T, R> => R apply(T t) ———— 接受一個T類型的參數,返回R類型結果。
Function<Integer, String> function1 = (x) -> "result: " + x;
function1.apply(6);
(2)Consumer<T> => void accept(T t) ———— 接受一個T類型的參數,無返回。
Consumer<String> consumer = (x) -> System.out.println("consumer: " + x);
consumer.accept("Hello");
(3)Predicate<T> => boolean test(T t) ———— 接受一個T類型的參數,返回布爾值。
Predicate<String> predicate = (x) -> x.length() > 0;
predicate.test("String");
(4)Supplier<T> => T get() ———— 無輸入參數,返回T類型的一個結果。
Supplier<String> supplier = () -> "Test supplier";
supplier.get();