How can I optimize this following code. I want to display per colleges, departments, courses and students who are currently taking up such courses on my JTable.
public void display(){
...
for(College co: collegeDao.getColleges().stream().sorted(Comparator.comparing(College::getName)).collect(Collectors.toList())){
dtm.addRow(new Object[]{co.getName()});
for(Department department: deptDao.getDepartments().stream().sorted(Comparator.comparing(Department::getName)).collect(Collectors.toList())){
if(Objects.equals(department.getCollege().getId(), co.getId())){
dtm.addRow(new Object[]{null, department.getName()});
for(Course c: courseDao.getCourses().stream().sorted(Comparator.comparing(Course::getName)).collect(Collectors.toList())){
if(Objects.equals(c.getDepartment().getId(), department.getId())){
dtm.addRow(new Object[]{null, null, c.getName()});
for(Student st: cTakenDao.getCoursesTaken(c).stream().sorted(Comparator.comparing(Student::getName)).collect(Collectors.toList())){
dtm.addRow(new Object[]{null, null, null, st.getName()});
counter++;
}
}
}
dtm.addRow(new Object[]{null, null, null, null, "Maxumim Population Allowed "+department.getPopulation()});
dtm.addRow(new Object[]{null, null, null, null, "Remaining Slot "+(department.getPopulation()-counter)});
counter = 0;
}
}
}
...
It actually works, but it takes time to load my data on the table. If I use LEFT JOIN on my sql query, all of my data were being repeated on upon display on my JTable.
Hope someone can suggest better solution of it, thank you in advance.