我有两个不同的列表,第一个是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={}}
1条答案
按热度按时间quhf5bfb1#
这里的主要问题是代码的不完整部分不能表示最小的可复制示例,相关的类和部分丢失(例如。,
StudentImpl
以及vakkenLijst
,Vak
).可能需要进行以下重构:
在课堂上
Student
提供模块列表(可能不是冗余的failOrPass
Map)并将其包含到toString()
方法:在中实现类复制构造函数
Module
从一个示例Vak
班级:重命名方法
getPassedAndFaillModule
-getter不应该void
返回类型:然后对于缩短的测试数据(没有不必要的细节)
Student
)结果如下:输出: