Java 四大函數式接口

Java四大函數式接口

函數式接口: 只有一個方法的接口

Consumer<T>消費型

給定一個參數,沒有返回值

void accept(T t);

Consumer<String> c = (x) -> System.out.println("Hello World");

Supplier<T>提供型

沒有輸入的參數,返回一個類型

T get();

Supplier<Integer> su = () -> 12;

Function<T,R>函數型

給定一個參數,並返回一個值

R apply(T t);

Function<String, String> fun = (x) -> x.toUpperCase();

Predicate<T>斷定型

給定一個輸入參數,返回一個布爾值

booolean test(T t);

Predicate<String> pre = (str) -> str.length()>2;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章