Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut for list->stream->map()->list

I often convert lists like that

myList.stream().map(el -> el.name).collect(Collectors.toList())

is there any shorter version for this?

like image 463
wutzebaer Avatar asked Sep 29 '19 11:09

wutzebaer


1 Answers

You could statically import Collectors.* and then use the mapping(Function, Collector) method, like this:

myList.stream().collect(mapping(T::getName, toList()));

Where T::getName is a method reference and T is the Type of the elements in the List. Using this is more readable and also almost identical to writing: el -> el.name

like image 191
Lino Avatar answered Oct 11 '22 00:10

Lino