I'm wondering if there is a quick/clean way to get the symmetric difference between two sets ?
I have:
Set<String> s1 = new HashSet<String>();
s1.add("a");
s1.add("b");
s1.add("c");
Set<String> s2 = new HashSet<String>();
s2.add("b");
I need something like:
Set<String> diff = Something.diff(s1, s2);
// diff would contain ["a", "c"]
Just to clarify I need the symmetric difference.
You want the symmetric difference.
If you want a library, Apache Commons CollectionUtils has
which returns a non-generic
Collection
.and Guava Sets has
which returns an unmodifiable
Set
as a genericSets.SetView
.Guava is a bit more modern, supporting generics, but either of these will work.