In Java (maybe using Guava?), is there some method provided to get the difference of two Collection
s, e.g. a List
and a Set
without modifying one of these Collection
s (else there would be collection1.removeAll(collection2)
?
In Guava there is Sets.difference(set1,set2)
, but it only works for Set
s, not for arbitrary collections.
Thanks for any hint!
ApacheCommons CollectionUtils has a method named disjuction that
Returns a Collection containing the exclusive disjunction (symmetric difference) of the given Iterables
You can filter the first Collection
using built-in Predicate
s:
Collections2.filter(c1, Predicates.not(Predicates.in(c2))
It works with any kind of Collection
s, but obviously it's better if c2
is a Set
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With