java Function函数中的BinaryOperator<T>接口用于执行lambda表达式并返回一个T类型的返回值,下面的BinaryOperator用法示例让你简单了解一下。
import java.util.function.BinaryOperator;
public class TestDemo {
public static void main(String[] args) {
BinaryOperator<Integer> add = (n1, n2) -> n1 + n2;
//apply方法用于接收参数,并返回BinaryOperator中的Integer类型
System.out.println(add.apply(3, 4));
}
}
返回结果为:7
当然了,也可以用来操作字符串的lambda表达式,如下。
public class TestDemo {
public static void main(String[] args) {
BinaryOperator<String> addStr = (n1, n2) -> n1 +"==="+ n2;
//apply方法用于接收参数,并返回BinaryOperator中的String类型
System.out.println(addStr.apply("3", "4"));
}
}
minBy方法使用:
import java.util.Comparator;
import java.util.function.BinaryOperator;
public class TestDemo {
public static void main(String[] args) {
BinaryOperator<Integer> bi = BinaryOperator.minBy(Comparator.naturalOrder());
System.out.println(bi.apply(2, 3));
}
}
返回结果为:2 |
|
maxBy方法使用:
public class TestDemo {
public static void main(String[] args) {
BinaryOperator<Integer> bi = BinaryOperator.minBy(Comparator.naturalOrder());
System.out.println(bi.apply(2, 3));
}
}
java.util.function中 Function, Supplier, Consumer, Predicate和其他函数式接口广泛用在支持lambda表达式的API中。这些接口有一个抽象方法,会被lambda表达式的定义所覆盖。
@FunctionalInterface
public interface BiFunction<T, U, R> {
/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
R apply(T t, U u);
}
BiFunction 接受两个参数 返回一个结果
求两个数的 四则运算:
public static Integer getSum(Integer a, Integer b, BiFunction<Integer, Integer, Integer> biFunction) {
return biFunction.apply(a, b);
}
public static void main(String[] args) {
System.out.println(getSum(1,2,(a,b)->a+b));
System.out.println(getSum(1,2,(a,b)->a-b));
System.out.println(getSum(1,2,(a,b)->a*b));
System.out.println(getSum(2,2,(a,b)->a/b));
}
-------Collectors 类的 toMap()
方法
在使用 java.util.stream.Collectors
类的 toMap()
方法转 Map 时,一定要使用含有参数类型为 BinaryOperator,参数名为 mergeFunction 的方法,否则当出现相同 key 值时会抛出 IllegalStateException 异常。
String[] departments = new String[] {"王二", "王二", "王三"};
Map<Integer, String> map = Arrays.stream(departments)
.collect(Collectors.toMap(String::hashCode, str -> str));
运行这段代码时,会抛出异常,下面是堆栈信息:
Exception in thread "main" java.lang.IllegalStateException: Duplicate key 867758096 (attempted merging values 王二 and 王二)
at java.base/java.util.stream.Collectors.duplicateKeyException(Collectors.java:133)
at java.base/java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$1(Collectors.java:180)
key 重复了,因为两个“王二”的 hashCode 相同,那这时候的解决办法呢?
String[] departments = new String[] {"王二", "王二", "王三"};
Map<Integer, String> map = Arrays.stream(departments)
.collect(Collectors.toMap(String::hashCode, str -> str, (v1, v2) -> v2));
多加个参数 (v1, v2) -> v2
,也就是重复的时候选一个。来看看此时调用的toMap()
方法吧。
public static <T, K, U>
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction) {
return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}