I often convert lists like that
myList.stream().map(el -> el.name).collect(Collectors.toList())
is there any shorter version for this?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With