Java 8 lambda create list of Strings from list of objects
One more way of using lambda collectors like above answers
List<String> tmpAdresses= users
.stream()
.collect(Collectors.mapping(User::getAddress, Collectors.toList()));
It is extended your idea:
List<String> tmpAdresses = users.stream().map(user ->user.getAdress())
.collect(Collectors.toList())
You need to collect
your Stream
into a List
:
List<String> adresses = users.stream()
.map(User::getAdress)
.collect(Collectors.toList());
For more information on the different Collectors
visit the documentation.
User::getAdress
is just another form of writing (User user) -> user.getAdress()
which could as well be written as user -> user.getAdress()
(because the type User
will be inferred by the compiler)