Home Java - Getting Difference Between Two Lists

# Java - Getting Difference Between Two Lists

user954
1#
user954 Published in March 19, 2018, 6:22 pm
 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 coaches = new ArrayList<>(); coaches.add("Josh"); coaches.add("Jake"); coaches.add("Tyler"); List 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?
 You need to login account before you can post.
Processed in 0.341298 second(s) , Gzip On .