Finding the Difference Between Two Java Lists

Learn how to find differences between two Java Collections using plain Java, Streams API, Google Guava, and Apache Commons Collections.

Overview

We often must compare two List instances and find elements that exist in the first List but not in the other. The comparison happens between two Collections of the same type. Consider a List containing all Employees of an organization and another List containing vaccinated Employees only, and we want to find out all the Employees who are not vaccinated. That means from the first List, we should remove all the employees that exist in the second List. The remaining Employees have not been vaccinated.

In Java, however, there is no straightforward way to find the difference between two Collections. Thankfully there are a few easy-to-use workarounds that we are going to learn in the following sections.

Consider we have these two Lists where some elements are common. We use different techniques to find the differences between these two ArrayLists.

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

Using Plain Java

The List interface provides the removeAll() method that subtracts all the elements of the given List from this List.

Example of List.removeAll() method to remove all the elements common with the other.

List<String> difference = new ArrayList<>(one);
difference.removeAll(two);

System.out.println(difference);
//prints:
//[Tim, Leo, Tim]Code language: Java (java)

We copied all the elements of one List into the output and then removed all the elements common to the List two using the removeAll() method.

We can flip the sides to find the elements of the second List that do not exist in the first List.

List<String> difference = new ArrayList<>(two);
difference.removeAll(one);

System.out.println(difference);
//prints:
//[Mia, Lyn, Zoe, Mia]Code language: Java (java)

Using Java Streams

Alternatively, we can use the Java Streams filter() operation to filter out Stream elements based on a Predicate.

Example of using Streams filter() to find the difference between two Java Lists.

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

We can also flip the sides here two find the differences the other way around.

List<String> difference = two.stream()
    .filter(e -> !one.contains(e))
    .toList();
System.out.println(difference);
//prints:
//[Mia, Lyn, Zoe, Mia]Code language: Java (java)

Using Apache Commons

The Apache Commons Collections library contains useful abstractions and extensions to the Java Collections API. The CollectionUtils class provides a utility method that returns elements of the first List that do not exist in the second List.

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

Similar to our previous two examples, we can change the argument’s order to the removeAll() method to see the differences the other way around.

Using Guava

Google Guava is a popular open-source library containing utility methods and abstractions. The Sets class of the library provides the difference() method that returns the difference between two Java Sets. That means we need to convert our Lists to Sets.

Example of using Guava to find the difference between two Java List instances.

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

The difference method returns an instance of a Set containing the elements that exist only in the first instance and not in the other.

Summary

This tutorial taught us how to find the difference between two Lists in Java. We saw several examples of finding elements from a List that do not appear in the other List using plain Java, Java Streams API, Apache Commons Collections Library and Google Guava Library.

Please visit our GitHub Repository for the complete source code.

Read More: