Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need some help regarding this Lambda expression in java

I came across this statement while going through JSR-335 specifications:

(test ? list.map(String::length) : Collections.emptyList())::iterator

What does this list.map(String::length) mean?

like image 252
Nithin Avatar asked Sep 05 '26 21:09

Nithin


2 Answers

The map operation lets you apply a function, in this case, String::length to each argument in the list, and returns another collection comprised of the results of applying that function to each one of those list elements. In this case, a list of Integers.

Lets say I have a list that looks like:

{"Bob", "Mary", "Joe"}

then you'd get back:

{3, 4, 3}

after applying your particular mapping.

like image 58
Amir Afghani Avatar answered Sep 07 '26 13:09

Amir Afghani


I believe that's a reference to the length methods defined in java.lang.String. In this case, you're basically telling .map() to call the method on every member of the collection and return a new collection comprised of those lengths.

like image 39
Platinum Azure Avatar answered Sep 07 '26 13:09

Platinum Azure



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!