[英]What do we need the BiFunction interface for?
The definition of the BiFunction
interface contains a method apply(T t, U u)
, which accepts two arguments. BiFunction
接口的定义包含一个方法apply(T t, U u)
,它接受两个参数。 However, I don't understand the use or purpose of this interface and method.但是,我不了解此接口和方法的用途或目的。 What do we need this interface for?
我们需要这个接口做什么?
The problem with this question is that it's not clear whether you see the purpose of a Function
, which has a method apply(T t)
.这个问题的问题是不清楚你是否看到了
Function
的目的,它有一个方法apply(T t)
。
The value of all the functional types is that you can pass code around like data.所有函数类型的价值在于您可以像数据一样传递代码。 One common use of this is the callback , and until Java 8, we used to have to do this with anonymous class declarations:
它的一个常见用途是callback ,在 Java 8 之前,我们曾经不得不使用匿名类声明来做到这一点:
ui.onClick(new ClickHandler() {
public void handleAction(Action action) {
// do something in response to a click, using `action`.
}
}
Now with lambdas we can do that much more tersely:现在使用 lambdas 我们可以更简洁地做到这一点:
ui.onClick( action -> { /* do something with action */ });
We can also assign them to variables:我们还可以将它们分配给变量:
Consumer clickHandler = action -> { /* do something with action */ };
ui.onClick(clickHandler);
... and do the usual things we do with objects, like put them in collections: ...并做我们对对象所做的常见事情,例如将它们放入集合中:
Map<String,Consumer> handlers = new HashMap<>();
handlers.put("click", handleAction);
A BiFunction
is just this with two input parameters. BiFunction
就是带有两个输入参数的函数。 Let's use what we've seen so far to do something useful with BiFunctions
:让我们使用到目前为止我们所看到的来对
BiFunctions
做一些有用的BiFunctions
:
Map<String,BiFunction<Integer,Integer,Integer>> operators = new HashMap<>();
operators.put("+", (a,b) -> a + b);
operators.put("-", (a,b) -> a - b);
operators.put("*", (a,b) -> a * b);
...
// get a, b, op from ui
ui.output(operators.get(operator).apply(a,b));
One of usages of BiFunction
is in the Map.merge
method. BiFunction
一种用法是在Map.merge
方法中。
Here is an example usage of the Map.merge
method, which uses a BiFunction
as a parameter.这是
Map.merge
方法的示例用法,它使用BiFunction
作为参数。 What merge
does is basically replaces the value of the given key with the given value if the value is null or the key does not have a value.如果值为空或键没有值,
merge
所做的基本上是用给定值替换给定键的值。 Otherwise, replace the value of the given key after applying the BiFunction
.否则,在应用
BiFunction
后替换给定键的值。
HashMap<String, String> map = new HashMap<>();
map.put("1", null);
map.put("2", "Hello");
map.merge("1", "Hi", String::concat);
map.merge("2", "Hi", String::concat);
System.out.println(map.get("1")); // Hi
System.out.println(map.get("2")); // HelloHi
If a BiFunction
were not used, you would have to write a lot more code, even spanning several lines.如果不使用
BiFunction
,您将不得不编写更多代码,甚至跨越几行。
Here is a link that shows all the usages of BiFunction
in the JDK: https://docs.oracle.com/javase/8/docs/api/java/util/function/class-use/BiFunction.html这是一个链接,显示了 JDK 中
BiFunction
所有用法: https : BiFunction
Go check it out!去看看吧!
An extra example of BiFunction is reduce()
: BiFunction 的一个额外示例是
reduce()
:
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(Arrays.asList(5,5,10));
Integer reduce = list.stream().reduce(0, (v1,v2) -> v1+v2);
System.out.println(reduce); // result is: 20
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.