Java8新特性两个关联集合合并成父子级

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

public class TestFilter {
    public static void main(String[] args) {
        // 学生
        User user1 = new User("1", "张三", "no1", 27);
        User user2 = new User("2", "李四", "no2", 15);
        User user3 = new User("3", "王五", "no1", 24);
        User user4 = new User("4", "赵六", "no3", 15);
        User user5 = new User("5", "李七", "no1", 29);
        User user6 = new User("6", "钱八", "no3", 18);

        List<User> listUser = new ArrayList<>();
        listUser.add(user1);
        listUser.add(user2);
        listUser.add(user3);
        listUser.add(user4);
        listUser.add(user5);
        listUser.add(user6);

        // 学校
        School school1 = new School("no1", "国防科技大学", null);
        School school2 = new School("no2", "南京大学", null);
        School school3 = new School("no3", "西北工业大学", null);

        List<School> listSchool = new ArrayList<>();
        listSchool.add(school1);
        listSchool.add(school2);
        listSchool.add(school3);

        // 用Java8新特性 筛选出各个班级的学生
        List<School> resultList = listSchool.stream().map(m1 -> {
            // 根据学校Id获取合并当前学校的学生
            List<User> collect = listUser.stream().filter(
                    m2 -> Objects.equals(m1.getId(), m2.getSchoolId())).collect(Collectors.toList());
            m1.setListUser(collect);
            return m1;
        }).collect(Collectors.toList());
        System.out.println("resultList = " + resultList);
    }
}

class User {
    private String id;
    private String name;
    private String schoolId;
    private int age;

    public User() {

    }

    public User(String id, String name, String schoolId, int age) {
        this.id = id;
        this.name = name;
        this.schoolId = schoolId;
        this.age = age;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(String schoolId) {
        this.schoolId = schoolId;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class School {
    private String id;
    private String schoolName;
    private List<User> listUser;

    public School(String id, String schoolName, List<User> listUser) {
        this.id = id;
        this.schoolName = schoolName;
        this.listUser = listUser;
    }

    public String getId() {
        return id;
    }

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

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    public List<User> getListUser() {
        return listUser;
    }

    public void setListUser(List<User> listUser) {
        this.listUser = listUser;
    }
}

 

posted @ 2021-07-16 16:18  蛋挞小子  阅读(1054)  评论(0编辑  收藏  举报