'Java 8 lambda create list of Strings from list of objects
I have the following qustion:
How can I convert the following code snipped to Java 8 lambda style?
List<String> tmpAdresses = new ArrayList<String>();
for (User user : users) {
tmpAdresses.add(user.getAdress());
}
Have no idea and started with the following:
List<String> tmpAdresses = users.stream().map((User user) -> user.getAdress());
Solution 1:[1]
It is extended your idea:
List<String> tmpAdresses = users.stream().map(user ->user.getAdress())
.collect(Collectors.toList())
Solution 2:[2]
One more way of using lambda collectors like above answers
List<String> tmpAdresses= users
.stream()
.collect(Collectors.mapping(User::getAddress, Collectors.toList()));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Piotr Rogowski |
| Solution 2 | Oomph Fortuity |
