如何对项目进行分组并转换为 java 中的列表

how to group items and convert to list in java

我有如下DTO

public class UserDTO implements Serializable {
    private String id;
    private String fullName;
    private String phone;
    private String role;
    private List<String> roleList;

    public UserDTO(String id, List<String> roleList) {
        super();
        this.id = id;
        this.roleList = roleList;
    }
}

我有 UserDTO 的列表,其中 roleList 未填充,但 role 已填充。问题是 role 可能是多个,但不知何故我无法在单个查询中获取列表。所以,我想按 id 分组并生成 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());

提前致谢。

这可能适合你:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class UserDTO  {
    private String id;
    private String fullName;
    private String phone;
    private String role;
    private List<String> roleList;

    public UserDTO(String id,String fullName,String phone,String role) {
        this.id = id;
        this.fullName = fullName;
        this.phone = phone;
        this.role =role;
    }

    public UserDTO(UserDTO oldObject,String key, List<String> collect) {
        this.id = key;
        this.fullName = oldObject.getFullName();
        this.phone = oldObject.getPhone();
        this.roleList = collect;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public List<String> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<String> roleList) {
        this.roleList = roleList;
    }
    public static void main(String[] args) {

        ArrayList<UserDTO> list = new ArrayList<>();
        list.add(new UserDTO("ASD", "John", "12345", "user"));
        list.add(new UserDTO("ASD", "John", "12345", "admin"));
        list.add(new UserDTO("SDB", "Smith", "12345", "super user"));
        list.add(new UserDTO("SDB", "Smith", "12345", "admin"));
        list.add(new UserDTO("DFG", "Neo", "12345", "user"));
        Map<String, List<UserDTO>> resultsWithSameIdAndName = list.stream().collect(Collectors.groupingBy(UserDTO::getId));
        List<UserDTO> mergedResults = resultsWithSameIdAndName.entrySet().stream()
                .map(e -> new UserDTO(
                        e.getValue().get(0),
                        e.getKey(),
                        e.getValue().stream().map(UserDTO::getRole).collect(Collectors.toList())))
                .collect(Collectors.toList());
        System.out.println(mergedResults);

    }

}

我所做的唯一更改是在构造函数中添加一个参数,该参数采用 UserDTO 对象。

您的代码应该可以工作。我为此添加了另一种方法。

首先由用户 id

roleList 准备地图
Map<String, List<String>> resultsWithSameIdAndName = users.stream()
        .collect(Collectors.groupingBy(UserDTO::getId,
                 Collectors.mapping(d-> d.getRole(), Collectors.toList())));

然后按用户创建唯一用户列表id

List<UserDTO> uniqueUsers = users.stream()
    .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserDTO::getId))),ArrayList::new));

并从映射中获取角色列表并为每个用户设置

for(UserDTO user :uniqueUsers) {
    user.setRoleList(resultsWithSameIdAndName.get(user.getId()));
}