Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which FunctionalInterface should I use?

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?

like image 575
Mani Avatar asked Dec 22 '18 06:12

Mani


2 Answers

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.

like image 79
Naman Avatar answered Sep 22 '22 09:09

Naman


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;
like image 43
davidxxx Avatar answered Sep 21 '22 09:09

davidxxx