java8 按對象屬性值分組


Map<String, List<User>> userMap = list.stream().collect(Collectors.groupingBy(User::getGroup));

示例:

public class HelloWorld {

    public static void main(String[] args) {
        Random random = new Random();
        List<User> list = new ArrayList<>();
        for(int i=1;i<=20;i++) {
            String group = (random.nextInt(3) + 1) + "組";//1-3組隨機
            User u = new User(i, "用戶-" + i, group);
            list.add(u);
        }

        //按group屬性分組
        Map<String, List<User>> userMap = list.stream().collect(Collectors.groupingBy(User::getGroup));

        System.out.println("分組后:" + userMap);
    }
    private static class User{
        Integer id;
        String name;
        String group;

        public User(Integer id, String name, String group) {
            this.id = id;
            this.name = name;
            this.group = group;
        }

        public Integer getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getGroup() {
            return group;
        }

        public void setGroup(String group) {
            this.group = group;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", group='" + group + '\'' +
                    '}';
        }
    }

}

執行結果:

{
    3 組 = [User {
            id = 2, name = '用戶-2', group = '3組'
        }, User {
            id = 5, name = '用戶-5', group = '3組'
        }, User {
            id = 7, name = '用戶-7', group = '3組'
        }, User {
            id = 8, name = '用戶-8', group = '3組'
        }, User {
            id = 9, name = '用戶-9', group = '3組'
        }, User {
            id = 12, name = '用戶-12', group = '3組'
        }, User {
            id = 13, name = '用戶-13', group = '3組'
        }, User {
            id = 15, name = '用戶-15', group = '3組'
        }, User {
            id = 16, name = '用戶-16', group = '3組'
        }], 
    2 組 = [User {
            id = 1, name = '用戶-1', group = '2組'
        }, User {
            id = 17, name = '用戶-17', group = '2組'
        }, User {
            id = 18, name = '用戶-18', group = '2組'
        }, User {
            id = 20, name = '用戶-20', group = '2組'
        }], 
    1 組 = [User {
            id = 3, name = '用戶-3', group = '1組'
        }, User {
            id = 4, name = '用戶-4', group = '1組'
        }, User {
            id = 6, name = '用戶-6', group = '1組'
        }, User {
            id = 10, name = '用戶-10', group = '1組'
        }, User {
            id = 11, name = '用戶-11', group = '1組'
        }, User {
            id = 14, name = '用戶-14', group = '1組'
        }, User {
            id = 19, name = '用戶-19', group = '1組'
        }]
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM