How to get the list of items that are extra in one ArrayList when compared to another?
I have two ArrayList with the following it开发者_如何学Pythonems
ArrayList1 {"bc.jpg","12.jpg","xy.png","123.gif","ref.gif","gef.png","abc.jpg"}
ArrayList2 {"tt.jpg","12.jpg","xy.png","gef.png","abc.jpg"}
The items in the list may not be sorted
Is there a function to find out the extra items in ArrayList1 ("bc.jpg","123.gif","ref.gif") which are not present in ArrayList2?
Set<String> copy = new HashSet<String>(list1);
copy.removeAll(list2);
System.out.println(copy);
If you need to preserve the order and cardinality of the duplicates, you can use List/ArrayList instead of Set/HashSet but it will be less efficient.
精彩评论