0

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.

  • 5
    This question is better suited for CodeReview, not StackOverflow –  Mar 13 '18 at 18:48
  • Using DISTINCT in combination with JOIN should be somewhat faster than this. To make it even better, you could also paginate the query once that is done, create indexes/keys on the matched columns etc. – Vasan Mar 13 '18 at 18:48
  • 1
    https://stackoverflow.com/questions/7457879/algorithm-to-optimize-nested-loops – David Mar 13 '18 at 18:48
  • 1
    I'm voting to close this question as off-topic because it better suits https://codereview.stackexchange.com/. – lexicore Mar 13 '18 at 20:41
  • 1
    @burrito77 It is not. The fact that the code is working means nothing. The code is far too short for any serious review and the OP asks for an optimization only. At the same time, it's perfectly fine here. https://codereview.meta.stackexchange.com/a/5778/14363 **Just because a question is said to have "working code" and seems to be on-topic on Code Review, doesn't mean that it is automatically off-topic for Stack Overflow. There are many questions that are on-topic on both sites.** – maaartinus Mar 14 '18 at 22:11
  • 3
    @lexicore It is okay to recommend the OP post on CR but in the future, please don't use Code Review as a reason to close a question. Evaluate the request and use a reason like *too broad*, *primarily opinion-based*, etc. Then you can mention to the OP that it can be posted on Code Review if it is [on-topic](https://codereview.stackexchange.com/help/on-topic). Please see the section **What you should not do** in [this answer to _A guide to Code Review for Stack Overflow users_](https://codereview.meta.stackexchange.com/a/5778/120114) – Sᴀᴍ Onᴇᴌᴀ Mar 14 '18 at 22:14

0 Answers0