3

Ive figured out how to compare the two ArrayLists and add the duplicates to a new ArrayList.

    ArrayList<Student> allStudentsA = assignStudents();
    ArrayList<Student> allStudentsB = allStudentsA;

    for (Student studentA : allStudentsA) {
        for (Student studentB : allStudentsB) {
            if (studentA.getId().equals(studentB.getId()) && studentA.getEduNumber() != studentB.getEduNumber()) {
                duplicateStudents.add(studentB);
            }
        }
    }

However done the way i do it, i add each duplicate once for everytime its there. Since "Rodaba" is there 7 times as she has 7 different priorities, she gets added to the list 7*6 times. Heres how i print out:

    for (Student student : duplicateStudents) {
        if (student.getFornavn().equals("Rodaba")) {
            System.out.println("Name: " + student.getFornavn() + "\t \t" + "EduNumber: " + student.getOptagelsesområde() + "\t" + "Prio: " + student.getPrio());
        }
    }

Is there a clever way i can avoid this, and only add "Rodaba" once for each priority she has applied for? Heres my output, is there a way to only get the marked section? Heres my output, is there a way to only get the marked section

I've been stuck on this for a long time. I'd really appreciate both suggestions on a better way to make the ArrayLists, as well as how to figure out this problem.

8
  • I think using Map could be a good solution. Also it eliminates nested for loops as well. Commented Jul 11, 2017 at 12:44
  • Why not just check if the student is already in the list? Also, it has no connection with this but you could override your Student#toString method to print it more cleanly Commented Jul 11, 2017 at 12:45
  • @Nathan The problem with that is that the student will be in the list n times where n is the amount of priorities they have. Commented Jul 11, 2017 at 12:48
  • @RohitMourya Can you elaborate on that? Commented Jul 11, 2017 at 12:49
  • @LarsChristensen Then, just check if it exists in the list with the same priority. Commented Jul 11, 2017 at 12:51

2 Answers 2

3

As I pointed out in the comments, you can simply check for the presence of one student before adding it:

ArrayList<Student> allStudentsA = assignStudents();
ArrayList<Student> allStudentsB = allStudentsA;

for (Student studentA : allStudentsA)
    for (Student studentB : allStudentsB)
        if (studentA.getId().equals(studentB.getId())
            && studentA.getEduNumber() != studentB.getEduNumber())
            if (!duplicateStudents.contains(studentB))
                duplicateStudents.add(studentB);

Note that this will only work if you overrode the equals and hashCode method of your Student class, as the objects do not have the same references.

Basically, you will check if the Student is already in the list before adding it. If you implemented correctly your equals method, a student A won't be equal to A with a different priority .

Sign up to request clarification or add additional context in comments.

4 Comments

Again the problem with this is, that it stops adding when it runs into the first student. So it only adds "Rodaba" once, on the 25th priority, where as what i want is to add it once for every priority.
@LarsChristensen Then it falls back on your Student#equals implementation: it seems your equals method returns true even when the priorities are differents.
You are a god Nathan, thats where the problem was. I edited my equals method to check properly for priorities, and the problem was solved.
@LarsChristensen Glad I could help, but I can't accept the god part -- this honor falls back to Jon Skeet, SO's Chuck Norris :D
1

You can use a different approach with streams. Eg:

List<Student> allStudentsA = assignStudents();
List<Student> duplicateStudents = allStudents.stream()
    .collect(groupingBy(Student::getId)) 
//Now you've got Map<String, List<Student>> (assuming id is of type String).
//Id of an user is a key. In value (list) you have all Students with the same id.
//Now we want to take this lists which have size greater than two and merge them.
    .values()
    .stream()
    .filter(list -> list.size() >= 2)
    .flatMap(List::stream)
    .collect(Collectors.toList());

(Improvements are welcome.)

1 Comment

Thank you for the alternative approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.