简体   繁体   English

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

[英]how to group items and convert to list in java

I have DTO like following我有像下面这样的 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;
    }
}

I have the list of UserDTO where roleList is not populated but role is populated.我有UserDTO列表,其中没有填充roleList列表,但填充了role The problem is role could be multiple but somehow i can't manage to get the list in a single query.问题是role可能是多个但不知何故我无法设法在单个查询中获取列表。 So, I want to group by id and generate roleList .所以,我想按 id 分组并生成roleList

For example, I have the following rows:例如,我有以下行:

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)

I have tried the following but it is not working (shouldn't though but i can't figure out what it should be) that's why i am seeking your help:我尝试了以下方法,但它不起作用(不应该,但我不知道它应该是什么)这就是我寻求你帮助的原因:

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());

Thanks in advance.提前致谢。

This might work for you:这可能对您有用:

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);

    }

}

Only changes that I done is added a parameter in constructor which takes an UserDTO object.只有我所做的更改是在构造函数中添加一个参数,该参数采用UserDTO object。

Your code should work.您的代码应该可以工作。 I added another way to this.我为此添加了另一种方法。

First prepare map for roleList by user id首先通过用户id为角色列表准备roleList

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

Then create unique user list by user id然后通过用户id创建唯一的用户列表

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

And get role list from map and set for every user并从 map 获取角色列表并为每个用户设置

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM