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

问题描述 投票:0回答:2

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

先谢谢你

java list group-by dto
2个回答
1
投票

你的代码应该能用。我又加了一个方法。

首先准备地图 roleList 用户 id

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

1
投票

这可能对你有用。

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 对象。

© www.soinside.com 2019 - 2024. All rights reserved.