如何使用java 8使用两个列表准备第三个列表

问题描述 投票:-1回答:1

我有两个不同的列表,并使用那些我准备使用流的第三个列表。

student.Java

public class Student {

    int id;
    String name;

    public Student(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

}

student lo次.Java

public class StudentLoc {

    int id;
    String loc;

    public StudentLoc(int id, String loc) {
        super();
        this.id = id;
        this.loc = loc;
    }

    public int getId() {
        return id;
    }

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

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

}

我有下面的第三堂课。

student D ETL OC.Java

public class StudentDetLoc {

    int id;
    String name;
        String Loc;

}
  1. 我必须使用id属性比较Student列表和StudentLoc列表。
  2. 如果id出现在两个列表中,那么我必须使用两个列表准备StudentDetLoc列表。
java collections
1个回答
2
投票

我的方法是:

  1. 使用streams()map()从第一个列表中创建一组学生ID
  2. 使用从步骤1获得的集合过滤filter()第二个列表
  3. 使用forEach()作为步骤2的终止操作并附加到最​​终的第3个列表(仅保留idnameLoc)。
© www.soinside.com 2019 - 2024. All rights reserved.