如何在ListObjectTwousRing流Java8上迭代listobjectone

plicqrtu  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(333)

我有两个不同的列表,第一个是list,第二个是list studentlist=newarraylist<>();list modulelist=new arraylist<>();
如何在studentlist上迭代modulelist,以便您可以看到哪个学生通过了哪个模块;
在此处输入图像描述
班级学生{

private String studentId;
private String name;
private Type gender;
private String studClass;
private String education;
private HashMap<String, Module> faillOrPass;

public Student()
{
    faillOrPass = new HashMap<String, Module>();
}

public Student (String studentId, String name, Type gender, String studClass, String education)
{
    faillOrPass = new HashMap<String, Module>();
    setStudentId(studentId);
    setName(name);
    setGender(gender);
    setStudClass(studClass);
    setEducation(education);
}

public enum Type { MALE, FAML}

// getters and setters
// equals() and hashCode()
// toString()

}
类模块{

String modulecode;
int grade;
int year;

public Module(String code, int year) {
    setModule(code);
    setYear(year);
    setGrade(-1);
}

// getters and setters
// equals and hashCode
// toString

}公共类testclass{

final static Student st = new Student();
static String ZAYNUL = st.setVolledigeNaam("Zaynul Zaini");
static String PIPO = st.setVolledigeNaam("Pipo");
static String SANNY = st.setVolledigeNaam("Sanny Saniwich");
static String JOHAN = st.setVolledigeNaam("Johan Ginger");
static String DURSAH = st.setVolledigeNaam("Dursah Pikobillo");
static String ROMAN = st.setVolledigeNaam("Roman Romanawich");
static String TOM = st.setVolledigeNaam("Johonson");
static String ANDRE = st.setVolledigeNaam("Andersons");

public static List<Vak> getModuleList() {
    return List.of(

            new Vak(ZAYNUL, "IT", 8, 2020),
            new Vak(SANNY, "IT", 10, 2019),
            new Vak(ROMAN, "IT", 9, 2018),
            new Vak(MALIKA, "IT", 8, 2017),
            new Vak(DURSAH,"IT", 10, 2016),
            new Vak(PIPO,"IT", 10, 2020),

            new Vak(PIPO,"IPC", 5, 2020),
            new Vak(ZAYNUL,"IPC", 6, 2019),
            new Vak(SANNY,"IPC", 4, 2018),
            new Vak(ROMAN,"IPC", 1, 2017),
            new Vak(MALIKA,"IPC", 5, 2016),
            new Vak(DURSAH,"IPC", 4, 2020));
}

public static List<Student> getStudentList() {
    List<Student> listVak = new ArrayList<>();
    listVak.add(new Student("011", ZAYNUL, Student.Type.MALE, "first year", "Architecture"));
    listVak.add(new Student("012", MALIKA, Student.Type.FEMALE, "second year", "economics"));
    listVak.add(new Student("013", SANNY, Student.Type.FEMALE, "third year", "IT"));
    listVak.add(new Student("014", ROMAN, Student.Type.MALE, "fourth year", "Astronomy"));
    listVak.add(new Student("015", TOM, Student.Type.MALE, "second year", "economics"));
    listVak.add(new Student("016", ANDRE, Student.Type.FEMALE, "third year", "IT"));
    listVak.add(new Student("017", JOHAN, Student.Type.MALE, "fourth year", "Astronomy"));
    return listVak;
}

public static void  getPassedAndFaillModule(List<Student> listSt, List<Module> listVak) {

    List<Student> filteredList = listSt.stream()
            .filter(student -> listVak.stream()
                    .filter(md -> md.getCijfer() > 5)
                    .anyMatch(mdo -> mdo.getStudentName() !=null
                            && student.getVolledigeNaam().equals(mdo.getStudentName())))
            .collect(Collectors.toList());
    filteredList.forEach(x -> System.out.println(x));

}

public static void main(String[] args) {
    System.out.println("Print the result: ");
    getPassedAndFaillModule(getStudentList(), getModuleList());
}

}
这是我得到的结果,但并不好。
打印结果:studentimpl{studentnummer='011',volledigenaam='zaynul zaini',type='male',klaas='first year',studierichting='architecture',vakkenlijst={}}
studentimpl{studentnummer='012',volledigenaam='malika ginger',type='female',klaas='second year',studierichting='economics',vakkenlijst={}}}
studentimpl{studentnummer='013',volledigenaam='sanny saniwich',type='female',klaas='third year',studierichting='it',vakkenlijst={}}
studentimpl{studentnummer='014',volledigenaam='roman romanawich',type='male',klaas='fourth year',Studieriching='astronomy',vakkenlijst={}}

quhf5bfb

quhf5bfb1#

这里的主要问题是代码的不完整部分不能表示最小的可复制示例,相关的类和部分丢失(例如。, StudentImpl 以及 vakkenLijst , Vak ).
可能需要进行以下重构:
在课堂上 Student 提供模块列表(可能不是冗余的 failOrPass Map)并将其包含到 toString() 方法:

class Student {
    private List<Module> modules;

    public List<Module> getModules() {
        return this.modules;
    }
    public void setModules(List<Module> list) {
        this.modules = list;
    }

    @Override
    public String toString() {
        return name + ": " + modules;
    }
}

在中实现类复制构造函数 Module 从一个示例 Vak 班级:

class Module {

    String module;
    int grade;
    int year;

    public Module(Vak vak) {
        this(vak.getModule(), vak.getGrade(), vak.getYear());
    }

    public Module(String code, int grade, int year) {
        setModule(code);
        setGrade(grade);
        setYear(year);
    }
// getters/setters/etc.

    @Override
    public String toString() {
        return String.format("Module: %s, grade=%d, year=%d", module, grade, year);
    }
}

重命名方法 getPassedAndFaillModule -getter不应该 void 返回类型:

public static void fillVakDataForStudents(List<Student> students, List<Vak> vaks) {
    students.forEach(s ->
        s.setModules(
            vaks.stream()
                //.filter(v.getGrade() > 5) // optionally filter by grade
                .filter(v -> v.getStudentName().equals(s.getName()))
                .map(Module::new) // using copy-like constructor
                .collect(Collectors.toList())
        )
    );
    students.forEach(System.out::println);
}

然后对于缩短的测试数据(没有不必要的细节) Student )结果如下:

static String ZAYNUL = "Zaynul Zaini";
static String PIPO = "Pipo";
static String SANNY = "Sanny Saniwich";
static String JOHAN = "Johan Ginger";
static String DURSAH = "Dursah Pikobillo";
static String ROMAN = "Roman Romanawich";
static String TOM = "Johonson";
static String ANDRE = "Andersons";

public static List<Vak> getVakList() {
    return List.of(
        new Vak(ZAYNUL, "IT", 8, 2020),
        new Vak(SANNY, "IT", 10, 2019),
        new Vak(ROMAN, "IT", 9, 2018),
        new Vak(JOHAN, "IT", 8, 2017),
        new Vak(DURSAH,"IT", 10, 2016),
        new Vak(PIPO,"IT", 10, 2020),

        new Vak(PIPO,"IPC", 5, 2020),
        new Vak(ZAYNUL,"IPC", 6, 2019),
        new Vak(SANNY,"IPC", 4, 2018),
        new Vak(ROMAN,"IPC", 1, 2017),
        new Vak(JOHAN,"IPC", 5, 2016),
        new Vak(DURSAH,"IPC", 4, 2020)
    );
}

public static List<Student> getStudentList() {
    return List.of(
        new Student("011", ZAYNUL),
        new Student("012", PIPO),
        new Student("013", SANNY),
        new Student("014", ROMAN),
        new Student("015", TOM),
        new Student("016", ANDRE),
        new Student("017", JOHAN),
        new Student("018", DURSAH)
    );
}

// calling test method
fillVakDataForStudents(getStudentList(), getVakList());

输出:

Zaynul Zaini: [Module: IT, grade=8, year=2020, Module: IPC, grade=6, year=2019]
Pipo: [Module: IT, grade=10, year=2020, Module: IPC, grade=5, year=2020]
Sanny Saniwich: [Module: IT, grade=10, year=2019, Module: IPC, grade=4, year=2018]
Roman Romanawich: [Module: IT, grade=9, year=2018, Module: IPC, grade=1, year=2017]
Johonson: []
Andersons: []
Johan Ginger: [Module: IT, grade=8, year=2017, Module: IPC, grade=5, year=2016]
Dursah Pikobillo: [Module: IT, grade=10, year=2016, Module: IPC, grade=4, year=2020]

相关问题