函數(shù)式接口
// 定義一個布爾表達式
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t)
}
// 接受兩個對象姊扔,返回boolean
BiPredicate<L,R> {
(L, R) -> boolean
}
// 訪問T類型的對象态兴,執(zhí)行某些操作邻眷,但不返回哮幢。
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
// 訪問T類型對象和R類型對象
BiConsumer<T, U> {
(T, U) -> void
}
// 接受輸入T類型對象,映射輸出R類型對象
@FunctionalInterface
public interface Function<T, R> {
R apply(T t)
}
// T和U兩個類型,映射成一個類型R
BiFunction<T, U, R> {
(T, U) -> R
}
// 類似工廠方法,不接受外部參數(shù)鄙信,制造一個T對象
Supplier<T> {
() -> T
}
// 一元操作符,進來T忿晕,出去也是T
UnaryOperator<T> {
T -> T
}
// 二元操作符
BinaryOperator<T> {
(T, T) -> T
}
復合Lambda表達式
比較器復合
inventory.sort(comparing(Apple::getWeight).reversed()
.thenComparing(Apple::getCountry)) // 兩個蘋果一樣重装诡,按國家排序
謂詞復合
Predicate<Apple> redAndHeavyAppleOrGreen =
redApple.and(a -> a.getWeight > 150)
.or( a -> "green".equals(a.getColor()));
函數(shù)復合
Function<String, String> addHeader = Letter::addHeader;
Function<String, String> transformationPipeline =
addHeader.andThen(Letter::checkSpelling)
.andThen(Letter::addFooter);
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者