java.util.function 包
1. Function
接收一个参数,并返回一个结果
2. BiFunction
接收两个参数,并返回一个结果
3. BinaryOperator
二元操作符,根据两个参数来返回一个计算结果。继承 BiFunction,具体写法上会更加简单,
1 | BinaryOperator<Integer> func = (n1, n2) -> n1 + n2; |
apply 就相当于是一个传参的过程,具体返回结果需要看 func 具体实现过程。
默认函数里实现了 minBy / maxBy 两种,可以自己实现 Comparator 然后传到方法中。
1 | public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) { |
4. Predicate
对某种类型的数据进行判断,从而得到一个boolean值结果
4.1 test
Predicate 接口中包含一个抽象方法: boolean test(T t)
1 | import java.util.function.Predicate; |
条件判断的标准是传入的Lambda表达式逻辑
4.2 and 与
1 | 源码: |
1 | import java.util.function.Predicate; |
4.3 or 或
1 | 源码: |
4.4 negate 取反
1 | default Predicate<T> negate() { |