简体   繁体   English

我应该使用哪个FunctionalInterface?

[英]Which FunctionalInterface should I use?

I was learning to write some lambda representation as FunctionalInterface . 我正在学习写一些lambda表示作为FunctionalInterface So, to add two integers I used: 所以,要添加我使用的两个整数:

BiFunction<Integer, Integer, Integer> biFunction = (a, b) -> a + b;
System.out.println(biFunction.apply(10, 60));

Gives me the output 70 . 给我输出70 But if I write it as this 但如果我这样写的话

BinaryOperator<Integer, Integer, Integer> binaryOperator = (a, b) -> a + b;

I get an error saying 我收到一个错误说

Wrong number of type arguments: 3; 错误的类型参数数量:3; required: 1 要求:1

Isn't BinaryOperator a child of BinaryFunction ? BinaryOperator不是BinaryFunction的子代吗? How do I improve it? 我该如何改进?

BinaryOperator

Since BinaryOperator works on a single type of operands and result . 由于BinaryOperator处理单一类型的操作数和结果 ie BinaryOperator<T> . BinaryOperator<T>

Isn't BinaryOperator a child of BinaryFunction? BinaryOperator不是BinaryFunction的子代吗?

Yes. 是。 BinaryOperator does extends BiFunction . BinaryOperator确实extends BiFunction But do note the documentation states(formatting mine): 但请注意文档说明(格式化我的):

This is a specialization of BiFunction for the case where the operands and the result are all of the same type . 这是BiFunction适用于操作数和结果都是相同类型的情况

The complete representation is as: 完整的表示如下:

BinaryOperator<T> extends BiFunction<T,T,T>

hence your code shall work with 因此你的代码应该使用

BinaryOperator<Integer> binaryOperator = (a, b) -> a + b;
System.out.println(binaryOperator.apply(10, 60));

IntBinaryOperator IntBinaryOperator

If you're supposed to be dealing with two primitive integers as currently in your example ( add two integers I used ), you can make use of the IntBinaryOperator FunctionalInterface as 如果你应该在你的例子中处理两个原始整数( 添加我使用的两个整数 ),你可以使用IntBinaryOperator FunctionalInterface作为

IntBinaryOperator intBinaryOperator = (a, b) -> a + b;
System.out.println(intBinaryOperator.applyAsInt(10, 60));

Represents an operation upon two int -valued operands and producing an int-valued result. 表示对两个int值的操作数进行的操作,并生成一个int值结果。 This is the primitive type specialization of BinaryOperator for int . 这是用于intBinaryOperator原始类型 BinaryOperator


I am using Integer, can I still use IntBinaryOperator 我使用Integer,我仍然可以使用IntBinaryOperator

Yes, you can still use it but notice the representation of the IntBinaryOperator 是的,您仍然可以使用它, 但请注意IntBinaryOperator的表示

Integer first = 10;
Integer second = 60;
IntBinaryOperator intBinaryOperator = new IntBinaryOperator() {
    @Override
    public int applyAsInt(int a, int b) {
        return Integer.sum(a, b);
    }
};
Integer result = intBinaryOperator.applyAsInt(first, second); 

would incur you an overhead of unboxing first and second to primitives and then autoboxing the sum as an output to result of type Integer . 会产生firstsecond基元拆箱的开销,然后将总和作为输出自动装箱Integer类型的result

Note : Be careful of using null-safe values for the Integer though or else you would probably end up with a NullPointerException . 注意 :注意尽量使用Integer null安全值 ,否则你最终可能会遇到NullPointerException

BiFunction<Integer, Integer, Integer> biFunction = (a, b) -> a + b;

can be represented by 可以用。来表示

BinaryOperator<Integer> binaryOperator = (a, b) -> a + b;

But generally you want to perform arithmetical computations on int and not Integer in order to avoid unboxing to compute (Integer to int) and boxing again to return the result (int to Integer) : 但通常你想对int而不是Integer执行算术计算,以避免拆箱计算(Integer to int)和再次装箱以返回结果(int to Integer):

IntBinaryOperator intBinaryOperator = (a, b) -> a + b;

As a side note, you could also use a method reference instead of a lambda to compute a sum between two int s. 作为旁注,您还可以使用方法引用而不是lambda来计算两个int之间的总和。
Integer.sum(int a, int b) is what you are looking for : 你正在寻找Integer.sum(int a, int b)

IntBinaryOperator biFunction = Integer::sum;

Isn't BinaryOperator a child of BinaryFunction? BinaryOperator不是BinaryFunction的子代吗?

Yes, it is. 是的。 If you look at source code of BinaryOperator , you see: 如果你看一下BinaryOperator源代码,你会看到:

public interface BinaryOperator<T> extends BiFunction<T,T,T> {
    // ...
}

So you just have to fix your syntax: 所以你只需要修复你的语法:

BinaryOperator<Integer> binaryOperator = (a, b) -> a + b;
System.out.println(binaryOperator.apply(10, 60));

How do I improve it? 我该如何改进?

You can use IntBinaryOperator . 您可以使用IntBinaryOperator It simplifies sytax even more: 它甚至可以简化sytax:

IntBinaryOperator binaryOperator = (a, b) -> a + b;
System.out.println(binaryOperator.applyAsInt(10, 60));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM