Finding the Differences Between Two Lists in Java

1. Overview

Finding differences between collections of objects of the same data type is a common programming task. As an example, imagine we have a list of students who applied for an exam and another list of students who passed it. The difference between those two lists would give us the students who didn’t pass the exam.

In Java, there’s no explicit way for finding differences between two lists in the List API, though there are some helper methods that come close.

In this quick tutorial, we’ll look at how to find the differences between the two lists. We’ll try a few different approaches, including plain Java (with and without Streams) and using third-party libraries such as Guava and the Apache Commons Collections.

2. Test Setup

Let’s start by defining two lists, which we’ll use to test out our examples:

public class FindDifferencesBetweenListsUnitTest {

    private static final List listOne = Arrays.asList("Jack", "Tom", "Sam", "John", "James", "Jack");
    private static final List listTwo = Arrays.asList("Jack", "Daniel", "Sam", "Alan", "James", "George");

}

3. Using the Java List API

We can create a copy of one list and then remove all the elements common with the other, using the List method removeAll():

List<String> differences = new ArrayList<>(listOne);
differences.removeAll(listTwo);
assertEquals(2, differences.size());
assertThat(differences).containsExactly("Tom", "John");

Let’s reverse this to find the differences the other way around:

List<String> differences = new ArrayList<>(listTwo);
differences.removeAll(listOne);
assertEquals(3, differences.size());
assertThat(differences).containsExactly("Daniel", "Alan", "George");

We should also note that if we want to find the common elements between the two lists, List also contains a retainAll method.

4. Using the Streams API

A Java Stream can be used for performing sequential operations on data from collections, which includes filtering differences between lists:

List<String> differences = listOne.stream()
            .filter(element -> !listTwo.contains(element))
            .collect(Collectors.toList());
assertEquals(2, differences.size());
assertThat(differences).containsExactly("Tom", "John");

As in our first example, we can switch the order of lists to find the different elements from the second list:

List<String> differences = listTwo.stream()
            .filter(element -> !listOne.contains(element))
            .collect(Collectors.toList());
assertEquals(3, differences.size());
assertThat(differences).containsExactly("Daniel", "Alan", "George");

We should note that the repeated calling of List.contains() can be a costly operation for larger lists.

5. Using Third-Party Libraries

5.1. Using Google Guava

Guava contains a handy Sets.difference method, but to use it we need to first convert our List to a Set:

List<String> differences = new ArrayList<>(Sets.difference(Sets.newHashSet(listOne), Sets.newHashSet(listTwo)));
assertEquals(2, differences.size());
assertThat(differences).containsExactlyInAnyOrder("Tom", "John");

We should note that converting the List to a Set will have the effect of deduplicating and reordering it.

5.2. Using Apache Commons Collections

The CollectionUtils class from Apache Commons Collections contains a removeAll method.

This method does the same as List.removeAll, while also creating a new collection for the result:

List<String> differences = new ArrayList<>((CollectionUtils.removeAll(listOne, listTwo)));
assertEquals(2, differences.size());
assertThat(differences).containsExactly("Tom", "John");

6. Handling Duplicate Values

Let’s now look at finding differences when two lists contain duplicated values.

To achieve this, we need to remove the duplicate elements from the first list, precisely as many times as they are contained in the second list.

In our example, the value “Jack” appears twice in the first list and only once in the second list:

List<String> differences = new ArrayList<>(listOne);
listTwo.forEach(differences::remove);
assertThat(differences).containsExactly("Tom", "John", "Jack");

We can also achieve this using the subtract method from Apache Commons Collections:

List<String> differences = new ArrayList<>(CollectionUtils.subtract(listOne, listTwo));
assertEquals(3, differences.size());
assertThat(differences).containsExactly("Tom", "John", "Jack");

7. Conclusion

In this article, we explored a few ways to find differences between lists.

In the examples, we covered a basic Java solutiona solution using the Streams API, and with third-party libraries like Google Guava and Apache Commons Collections.

We also saw how to handle duplicate values.

As always, the complete source code is available over on GitHub.

Related posts:

Predicate trong Java 8
Java – Reader to InputStream
Java Program to Implement LinkedList API
Base64 encoding và decoding trong Java 8
Form Validation with AngularJS and Spring MVC
Java Multi-line String
Java Program to Implement Quick Sort Using Randomization
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Hướng dẫn sử dụng Java Generics
Java Program to Decode a Message Encoded Using Playfair Cipher
How to Set TLS Version in Apache HttpClient
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Implement Gale Shapley Algorithm
Java Program to Implement Binary Tree
@Lookup Annotation in Spring
Upload and Display Excel Files with Spring MVC
Guide to Spring 5 WebFlux
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to subtract two large numbers using Linked Lists
Creating Docker Images with Spring Boot
Functional Interfaces in Java 8
Java Program to Implement EnumMap API
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Introduction to Spring Cloud Stream
Immutable Map Implementations in Java
Collect a Java Stream to an Immutable Collection
Từ khóa this và super trong Java
How to Replace Many if Statements in Java
Java Program to Implement the One Time Pad Algorithm
Java Program to Implement Disjoint Sets
Exploring the New Spring Cloud Gateway