In Java (maybe using Guava?), is there some method provided to get the difference of two Collections, e.g. a List and a Set without modifying one of these Collections (else there would be collection1.removeAll(collection2)?
In Guava there is Sets.difference(set1,set2), but it only works for Sets, 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 Predicates:
Collections2.filter(c1, Predicates.not(Predicates.in(c2))
It works with any kind of Collections, 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