What is a symmetric difference of two sets? It is a less common set-algebraic operation — the set of elements contained in either of two specified sets but not in both. The following code shows if you have two friend lists, a list of friends that is either yours or mine.

This snippet will find the difference between two collections specifically two sets using core JDK. We will first combine or add both sets together into yourFriendsOrMyFriends set. Then we will create temporary Set which we will call the retainAll and removeAll which will give us the difference of the two sets.

Google Guava

This snippet will find the difference between two sets using Guava Set Utility. Sets.symmetricDifference will return a set that contains all elements in either set1 or set2 but not in both.

Apache Commons

Using apache commons we will find the difference between sets. The CollectionUtils.disjunction will return a Collection containing the exclusive disjunction (symmetric difference) of the given Collections.