How to pass parameter in Stream.map method

What does the expression below mean:

String joined = elements.stream()
  .map(Object::toString)
  .collect(Collectors.joining(", "));

      

As I understand it Stream.map

takes a type parameter Function

. I don't understand how it goes Object::toString

and how does it work?

Thank.

+3


source to share


1 answer


How does Object::toString

it work and how does it work?

It is called a method reference , in which you tell it to use an existing method definition toString

(from the class Object

) for the methodmap

, instead of having you explicitly call obj -> obj.toString()

within the method map

.



I suggest you take a look here and understand how method references work in Java8.

+1


source







All Articles