Finding the Difference Between Two Java Sets

This article contains examples of finding the difference between two Java Set implementations.

Overview

Java Set is a collection of unique elements, and they are used in many use cases. Java doesn’t provide a straightforward way to compare two Java Set instances to find the differentiating elements between them.

This article demonstrates how we can use different techniques and libraries to find the difference between two Set instances.

Let’s consider the following two HashSet instances that we will compare and identify their differentiating elements.

Set<String> one = Set.of("Ray", "Tim", "Leo", "Bee"); Set<String> two = Set.of("Ray", "Mia", "Lyn", "Bee", "Zoe");
Code language: Java (java)

Using Plain Java

We will use a plain Java way to find the asymmetric differences between two Set instances. That means we will find the elements in the first Set which do not exist in the second.

Java Set’s removeAll() method removes all the elements from this Set that also exist in the given Set.

Example of finding the asymentric differences between two Java Sets in plain Java.

Set<String> difference = new HashSet<>(one); difference.removeAll(two); System.out.println(difference); //prints: //[Leo, Tim]
Code language: Java (java)

Please note that we have created a copy of the first Set to keep it unchanged.

Using Java Streams

Alternatively, we will find the asymmetric differences in two Java Sets using Java Streams API. To do so, we create a Stream of the first Set and use the filter() method to filter out the second Set elements.

Examples of finding the asymmetric differences in two Sets using Java Streams API.

Set<String> difference = one.stream() .filter(e -> !two.contains(e)) .collect(Collectors.toSet()); System.out.println(difference); //prints: //[Leo, Tim]
Code language: Java (java)

Using Guava Library

One of the useful abstractions of the open-source Google Guava Library is the Sets class. The differences() method of the class takes two Java Set instances and finds their asymmetric differences.

Example of using Guava Library to find the differences between two Java Sets.

Set<String> differences = Sets.difference(one, two); System.out.println(differences); //prints: //[Leo, Tim]
Code language: Java (java)

Using Commons Collections

Like the previous, the Apache Commons Collections library also provides an abstraction to find the asymmetric differences between two Java Lists. The removeAll() method of the CollectionUtils class returns a Collection instance containing the elements from the first Collection that do not exist in the second Collection.

Example of finding the asymmetric differences in two Java Sets using Commons Collections.

Set<String> differences = new HashSet<>( CollectionUtils.removeAll(one, two)); System.out.println(differences); //prints: //[Leo, Tim]
Code language: Java (java)

Symmetric Difference in Two Java Sets

Let’s understand how to find the symmetric differences in two Java Sets. The symmetric differences between any two Collections are the elements that exist in either of the Collections but not both.

One of the simplest ways of finding the symmetric difference between two Java Set instances is to use the disjunction() method of the CollectionUtils class in the Commons Collections library. The method takes two Java Collection instances and returns a Collection containing the symmetric differences between them.

Example of finding the symmetric differences in two Java Sets using Commons Collections.

Set<String> differences = new HashSet<>( CollectionUtils.disjunction(one, two)); System.out.println(differences); //prints: //[Lyn, Zoe, Mia, Leo, Tim]
Code language: Java (java)

We compared two Java Set instances and listed the elements that exist in either of the Set instances, not both.

Summary

This tutorial demonstrated various ways of finding the Symmetric and Asymmetric differences between two Java Set instances. We used plain Java, Java Stream API, Google Guava Library, and Apache Commons Collections Library in our examples.

The complete source code is available at our GitHub Repository.

More Like This: