用1.8流的形式优化以前的双层for循环

实体类

package loop;

public class UserInfo {
    private String id;
    private String name;
    private String schoolId;
    private String schoolName;

    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 String getSchoolName() {
        return schoolName;
    }

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

    @Override
    public String toString() {
        return "UserInfo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", schoolId='" + schoolId + '\'' +
                ", schoolName='" + schoolName + '\'' +
                '}';
    }

    public UserInfo(String id, String name, String schoolId, String schoolName) {
        this.id = id;
        this.name = name;
        this.schoolId = schoolId;
        this.schoolName = schoolName;
    }
}
package loop;

public class School {
    private String id;
    private String schoolName;

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

    @Override
    public String toString() {
        return "School{" +
                "id='" + id + '\'' +
                ", schoolName='" + schoolName + '\'' +
                '}';
    }

    public School(String id, String schoolName) {
        this.id = id;
        this.schoolName = schoolName;
    }
}

实现方法

package loop;

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

public class TestLoop {
    public static void main(String[] args) {
        // 学生集合
        List<UserInfo> userInfoList = new ArrayList<>();
        UserInfo userInfo1 = new UserInfo("1000", "张三", "2000", null);
        UserInfo userInfo2 = new UserInfo("1000", "李四", "2001", null);
        UserInfo userInfo3 = new UserInfo("1000", "王五", "2002", null);
        userInfoList.add(userInfo1);
        userInfoList.add(userInfo2);
        userInfoList.add(userInfo3);

        // 学校集合
        List<School> schoolList = new ArrayList<>();
        School school1 = new School("2000", "南京大学");
        School school2 = new School("2001", "东南大学");
        School school3 = new School("2002", "浙江大学");
        schoolList.add(school1);
        schoolList.add(school2);
        schoolList.add(school3);

        // 根据学校Id,将每个学生对应的学校名称放入(优化以前的双层for循环)
        List<UserInfo> list = userInfoList.stream()
                .map(userInfo -> schoolList.stream()
                        .filter(m1 -> Objects.equals(m1.getId(), userInfo.getSchoolId()))
                        .findAny()
                        .map(m2 -> {
                            userInfo.setSchoolName(m2.getSchoolName());
                            return userInfo;
                        }).orElse(userInfo))
                .collect(Collectors.toList());
        list.forEach(m1 -> {
            System.out.println(m1.toString());
        });
    }
}

 

posted @ 2022-04-13 14:59  蛋挞小子  阅读(136)  评论(0编辑  收藏  举报