mardi 1 septembre 2020

Double for loop with if condition

I am having trouble with double for loop in java, specifically inner if condition

Here is my code:

 public Set<Term> getAllTermsForStudent(Long id) {

        List<Term> terms = termRepository.findAll();
        Set<Term> set_terms = new HashSet<Term>(terms); //there are 3 terms
        Set<Exam> exams = studentService.getStudentExams(id); //in this student's case, there are 9 exams, 3 in each term
        Set<Exam> new_exams = new HashSet<>();

        for(Term t: set_terms) { 
            for(Exam e: exams) {
                if(t.getId() == e.getTerm().getId()) { //exam has term id, which indicates to which
                                                         term it belongs, so I'm comparing it, and I 
                                                         want to add it to new exam set
                    new_exams.add(e);
                }
            }
           t.setExams(new_exams);

        }

        return set_terms;
    }

Output of this function gives me 9 exams in each term, like it's ignoring if condition

Aucun commentaire:

Enregistrer un commentaire