Nithin
Nithin

Reputation: 45

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?

Upvotes: 3

Views: 74

Answers (2)

Amir Afghani
Amir Afghani

Reputation: 38541

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.

Upvotes: 3

Platinum Azure
Platinum Azure

Reputation: 46193

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.

Upvotes: 3

Related Questions