Reputation: 595
I am trying to implement a lambda Function2 in Java:
JavaPairRDD<String, Integer> reducedCounts = counts.reduceByKey(new Function2<Integer, Integer, Integer>() {
@Override
public Integer call(final Integer value0, final Integer value1) {
return Integer.valueOf(value0.intValue() + value1.intValue());
}
});
The above is a sample, but how to change it to a lamb8 format? I wanna define as a separate function
Function2<...> f2 = LAMBDA;
counts.reduceByKey(f2);
Upvotes: 1
Views: 2193
Reputation: 13793
You can go for a method reference which is the easiest option in this case:
Function2<Integer, Integer, Integer> f2 = Integer::sum;
counts.reduceByKey(f2)
which is equal to:
Function2<Integer, Integer, Integer> f2 = (i1, i1) -> i1 + i2;
counts.reduceByKey(f2)
Also, such simplification is possible because you perform a lot of unnecessary boxing/unboxing in order to calculate the sum of two integers.
Upvotes: 3
Reputation: 28143
Since Function2
is a functional interface, you can implement it with a simple lambda expression:
Function2<Integer,Integer,Integer> plus = (x,y)->Integer.valueOf(x.intValue()+y.intValue());
If your logic is more than a simple expression, you can use a block:
Function2<Integer, Integer, Integer> plus = (x, y) -> {
// do stuff
return Integer.valueOf(x.intValue()+y.intValue());
};
In case of a simple integer addition, you can take advantage of auto-unboxing and write this more simply as:
Function2<Integer, Integer, Integer> plus = (x, y) -> x + y;
You can also take advantage of sum
method in Integer
class to use a method reference:
Function2<Integer, Integer, Integer> plus = Integer::sum;
Upvotes: 2