DEV Community

Cover image for What is `map` in a Java Stream

What is `map` in a Java Stream

Gunnar Gissel on June 13, 2018

Originally published at www.gunnargissel.com Mapping is how to convert one type of object to another with a stream. Say you have a set of Fruit a...
Collapse
 
mbtts profile image
mbtts

Good article and thank you for sharing.

For those not aware it is also possible to use a method reference for the first example:

fruitList.stream()
         .map(Fruit::getName)
         .collect(toList());

This is just a more succinct syntax for invoking the #getName method on each and every instance of Fruit in the stream. As such even though it may appear to be invoked on the class it is not a static method.

Also a small API thing - it is Collectors#toList not #asList (and #toSet).

Collapse
 
andrekelvin profile image
AndreKelvin

Thanks. I now fully understand this mapping thing