In Java (possibly using Guava?) Is there any method to get the difference between two Collection s, for example. a List and a Set without changing one of these Collection (otherwise would it be collection1.removeAll(collection2) ?
Collection
List
Set
collection1.removeAll(collection2)
Guava has Sets.difference(set1,set2) , but it only works for Set s, and not for arbitrary collections.
Sets.difference(set1,set2)
Thanks for any hint!
You can filter the first Collection using the built-in Predicate s:
Predicate
Collections2.filter(c1, Predicates.not(Predicates.in(c2))
It works with any type of Collection s, but it is obviously better if c2 is Set .
c2
ApacheCommons CollectionUtils has a disjuction method which
Returns a collection containing an exceptional disjunction (symmetric difference) of Iterables data