Issue
I have task:
Return a map of companies, where the key is its name and the value is a list of employees stored as String consisting of firstName and lastName separated by space.
Here is my solution and its works fine:
Map<String, List<String>> getUserPerCompanyAsString() {
return getCompanyStream()
.collect(Collectors.toMap(Company::getName, company -> getUserNames(company.getUsers())));
}
private List<String> getUserNames(List<User> users) {
return users.stream()
.map(user -> user.getFirstName() + " " + user.getLastName())
.collect(Collectors.toList());
}
public class Company {
private final String name;
private final List<User> users;
}
public class User {
private final String firstName;
private final String lastName;
private final Sex sex;
private final int age;
private final List<Account> accounts;
private final List<Permit> permits;
}
So my question, how to convert List<User> as map value to List<String> with firstName and lastName separated by space in one stream chain without helper method?
Solution
Solution
Thanks to Thomas suggestion I'm able solve this problem without helper method:
Map<String, List<String>> getUserPerCompanyAsString() {
return getCompanyStream()
.collect(Collectors.toMap(Company::getName, company -> company.getUsers().stream()
.map(user -> user.getFirstName() + " " + user.getLastName())
.collect(Collectors.toList())));
Answered By - Fleckinger
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.