How to create a Function2 instance using lambdas in Java? -
i trying implement 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 sample, how change lamb8 format? wanna define separate function
function2<...> f2 = lambda; counts.reducebykey(f2);
you can go method reference easiest option in case:
function2<integer, integer, integer> f2 = integer::sum; counts.reducebykey(f2)
which equal to:
function2<integer, integer, integer> f2 = (i1, i1) -> i1 + i2; counts.reducebykey(f2)
also, such simplification possible because perform lot of unnecessary boxing/unboxing in order calculate sum of 2 integers.
Comments
Post a Comment