1

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);
1
  • 2
    counts.reduceByKey( (x,y) -> x + y) Commented Aug 18, 2017 at 9:10

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Luckylukee I believe this is exactly what you are looking for :)
Very clean. I was about to misstep by writing a lambda. Must remember method references!!
2

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;

1 Comment

any advice on this , how to handle this UDF issue stackoverflow.com/questions/63935600/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.