leozilla leozilla - 3 years ago 115
Java Question

How to: Find the Set Difference Between Two Collections in Java

I am relativly new to Java and I am wondering if there is a method like LINQs "Except" in C# to get the different items of two sets.

I looked at

CollectionUtils
from apache common,
Collections
and
Collections2
from guava but found no such method.

Btw: I am using Java 7 not Java 8.

Answer Source

I have used Guava Sets.difference.

The parameters are sets and not general collections, but a handy way to create sets from any collection (with unique items) is Guava ImmutableSet.copyOf(Iterable).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download