Comparing Two Lists In Java

This article demonstrates ways of comparing two Java Lists to find the common elements or missing elements.

Overview

We often use Java Lists as a general-purpose collection of ordered elements. We often need to compare two Java Lists for equality or to find their common or differentiating elements. Consider we have two Lists of Student instances, one that contains Math toppers and the other contains Sports toppers. We may want to compare the two Lists to find the Students who equally perform well in Sports and Maths or the Sports toppers which are poor in Maths.

There is no direct way to compare Java Lists to find their commonality or particularity in Java. However, there are a couple of workarounds that we are going to learn in the following sections.

Comparing Two Lists for Equality

We can use the equals() method from the List interface to compare two Java Lists for equality. As per the documentation, the equals() method returns true if:

  • Both Lists contain the same elements in the same order.
  • Both Lists have the same size.

Example of comparing Java Lists for equality

List<Integer> one = List.of(1, 2, 4, 6, 8);
List<Integer> two = List.of(1, 2, 4, 6, 8);
List<Integer> three = List.of(1, 2, 4, 6, 8, 4);

System.out.println(one.equals(two));
System.out.println(one.equals(three));

//prints:
//true
//falseCode language: Java (java)

Our article demonstrates how to assert using popular Unit Testing Frameworks if two java Lists are equal.

Common Elements in Two Lists

There are a few ways to find common elements between two Java Lists. Consider we have the following two List instances.

List<Integer> one = List.of(1, 2, 4, 6, 8);
List<Integer> two = List.of(3, 4, 5, 6, 7);Code language: Java (java)

Plain Java

The retainAll() method of Java List removes all elements from this List except those from the given List. We can use it to find the elements common to two ArrayLists.

Example of finding common elements between two Lists using plain Java.

List<Integer> commons = new ArrayList<>(one);
commons.retainAll(two);
System.out.println(commons);

//prints:
//[4, 6]Code language: Java (java)

The retainAll() method modifies this List. Hence we have made a copy of the List to keep it unchanged.

Java Streams

Alternatively, we can use Java Streams API to find common elements between two Lists. To do that, we can create a Stream of the first List and use the filter() method to retain only the elements in the second List.

Example of using Java Streams to find common elements between two Java Lists.

List<Integer> commons = one.stream()
    .filter(two::contains)
    .toList();
System.out.println(commons);

//prints:
//[4, 6]Code language: Java (java)

Apache Commons Collections

The CollectionUtils class in the Commons Collections library provides many useful abstractions, including the intersection() method. The method takes two Java Collections and returns a Collection instance containing their common elements.

Example of finding common elements of two Lists using Commons Collections.

List<Integer> commons = new ArrayList<>(
    CollectionUtils.intersection(one, two));
System.out.println(commons);

//prints:
//[4, 6]Code language: Java (java)

Difference between Two Lists

So far, we have been interested in finding the elements present in both Lists. Now let’s understand how to find the difference between two ArrayLists in Java.

Example of finding the difference between two Java Lists.

List<Integer> difference = new ArrayList<>(one);
difference.removeAll(two);
System.out.println(difference);

//prints:
//[1, 2, 8]Code language: Java (java)

Here, we are using the removeAll() method to find the elements of the first List that do not exist in the second. To keep the original List unchanged, we copied it before using the removeAll() method.

Please read our detailed article to learn how to find the difference between two Java Lists.

Summary

This quick tutorial described different ways of comparing two Java List instances. We compared two Java Lists for equality, listed their common elements, and identified their differences.

Please refer to our GitHub Repository for the complete source code.

More like this: