我有以下的DTO
public class UserDTO implements Serializable {
private String id;
private String fullName;
private String phone;
private String role;
private List<String> roleList;
}
I have the list of UserDTO
where roleList
is not populated but role
is populated. The problem is role
could be multiple but somehow i can't manage to get the list in a single query. So, I want to group by id and generate roleList
.
例如,我有以下几行:
id fullName phone role roleList
------------------------------------------------
abc Sarwar 017xxx admin NULL
abc Sarwar 017xxx operator NULL
I want it to be like the following
abc Sarwar 017xxx NULL ArrayList<String>(admin, operator)
我已经尝试了以下方法,但是它不起作用(虽然不能,但是我不知道应该怎么做),这就是为什么我寻求您的帮助:
Map<String, List<UserDTO>> resultsWithSameIdAndName = users.stream()
.collect(Collectors.groupingBy(r -> r.getId()));
List<UserDTO> mergedResults = resultsWithSameIdAndName.entrySet().stream()
.map(e -> new UserDTO(e.getKey(),
e.getValue().stream().map(r -> r.getRole()).collect(Collectors.toList())))
.collect(Collectors.toList());
提前致谢。