Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Lambda expression - have to cast args?

I'm going over examples on Spark RDDs and found the following snippet as an example:

JavaRDD distFile = sc.textFile("data.txt");
distFile.map(s -> s.toString().length()).reduce((a, b) -> a + b);

However, this code gives an error (straight from Netbeans):

bad operand types for binary operator '+'
first type: Object
second type: Object

Now, I fixed the problem by casting a and b to int (i.e. distFile.map(s -> s.toString().length()).reduce((a, b) -> (int) a + (int) b);), but I'm still wondering if there's a way to do this without casting or this is simply an error in their docs.

Thanks

like image 889
hummingBird Avatar asked Mar 05 '26 11:03

hummingBird


1 Answers

Have you tried mapToInt It converts the stream to IntStream.

like image 118
miskender Avatar answered Mar 08 '26 00:03

miskender