我想创建一个 BiFunction 对象 (Lambda),并在 Map 的计算方法中使用它。它会正确编译,但会在运行时抛出 NullPointerException。
private BiFunction<String, Integer, Integer> biFunctionWithAddition(final Integer addition) {
return (model, quantity) -> model == null ? addition : quantity + addition;
}
hashmap.compute(i, biFunctionWithAddition(1)) //throw NullPointerException
//the one that can work should be:
hashmap.compute(i, (num, quantity) -> num == null : 1 ? quantity + 1);
梦里花落0921
相关分类