I am trying to find the difference between two lists in Java. I am using this example (returning difference between two lists in java) that suggests to use removeAll()
to find the difference but that is not working. Instead, I seem to be getting a combined list of coaches
and teachers
.
Code:
List<String> coaches = new ArrayList<>();
coaches.add("Josh");
coaches.add("Jake");
coaches.add("Tyler");
List<String> teachers = new ArrayList<>();
coaches.add("Josh");
coaches.add("Jake");
coaches.removeAll(teachers);
for (String name : coaches) {
System.out.println("Name is: " + name);
}
Output:
Name is: Josh
Name is: Jake
Name is: Tyler
Name is: Josh
Name is: Jake
How would I check that teachers
is missing the value Tyler
so Tyler
would be returned?