7

I was learning to write some lambda representation as 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. 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; required: 1

Isn't BinaryOperator a child of BinaryFunction? How do I improve it?

Naman
  • 27,789
  • 26
  • 218
  • 353
Mani
  • 1,068
  • 3
  • 13
  • 27

3 Answers3

10

BinaryOperator

Since BinaryOperator works on a single type of operands and result. i.e. BinaryOperator<T>.

Isn't BinaryOperator a child of BinaryFunction?

Yes. BinaryOperator does 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.

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

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 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. This is the primitive type specialization of BinaryOperator for int.


I am using Integer, can I still use IntBinaryOperator

Yes, you can still use it but notice the representation of the 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.

Note: Be careful of using null-safe values for the Integer though or else you would probably end up with a NullPointerException.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Thank you for your answer. I am using `Integer`, can I still use `IntBinaryOperator`? – Mani Dec 22 '18 at 14:25
  • 1
    @Mani In that case, preferably use `BinaryOperator`, since `IntBinaryOperator` would add an overhead of autoboxing and unboxing. See the edit in the answer. – Naman Dec 22 '18 at 14:35
  • 1
    Ok. Thank you for the answer and explanation again. – Mani Dec 22 '18 at 14:43
6
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) :

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 ints.
Integer.sum(int a, int b) is what you are looking for :

IntBinaryOperator biFunction = Integer::sum;
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Thank you for your answer. I am using `Integer`, can I still use `IntBinaryOperator`? The other answer and document reads primitive type operation for it. – Mani Dec 22 '18 at 14:26
4

Isn't BinaryOperator a child of BinaryFunction?

Yes, it is. If you look at source code of BinaryOperator, you see:

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. It simplifies sytax even more:

IntBinaryOperator binaryOperator = (a, b) -> a + b;
System.out.println(binaryOperator.applyAsInt(10, 60));
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35