How to Compare Two Lists in Java

Learn to compare two ArrayList in Java to find if they contain equal elements. If both lists are unequal, we will find the difference between the lists. We will also learn to find common as well as different items in each list.

Note that the difference between two lists is equal to a third list which contains either additional elements or missing elements.

1. Comparing Two Lists for Equality

1.1. Sort then Compare

The following Java program tests if two given lists are equal. To test equality, we need to sort both lists and compare both lists using equals() method.

The List.equals() method returns true for two list instances if and only if:

  • both lists are of the same size
  • both contain the same elements in exactly the same order
ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
ArrayList<String> equalList = new ArrayList<>(Arrays.asList("c", "b", "a"));
ArrayList<String> diffList = new ArrayList<>(Arrays.asList("a", "b", "d")); //c and d are changed

Collections.sort(list);
Collections.sort(equalList);
Assertions.assertTrue(list.equals(equalList));

Collections.sort(diffList);
Assertions.assertFalse(list.equals(diffList));

1.2. Using Commons Collections

If you have commons-collections4 dependency in the project, we can use the CollectionUtils.isEqualCollection() API. This API compares the items from both lists, ignoring the order.

boolean result = CollectionUtils.isEqualCollection(firstList, secondList);

1.3. Comparing Lists in Unit Tests

If we are checking the list equality in unit tests, then consider using the Matchers.containsInAnyOrder().

Assertions.assertThat(firstList, Matchers.containsInAnyOrder( secondList.toArray() ));

See Also: Assert Two Equal Lists Ignoring Order

2. Compare Two Lists – Find Additional Items

In the following examples, we will find the items that are present in list1, but not in list2.

2.1. Plain Java

If two arraylists are not equal and we want to find what additional elements are in the first list compared to the second list, use the removeAll() method. It removes all elements of the second list from the first list and leaves only additional elements in the first list.

ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "e", "f"));

//additional items in listOne
listOne.removeAll(listTwo);

System.out.println(listOne);  //[c, d]

2.2. Using Stream API

We can iterate over the List items of the first list, and search all elements in the second list. If the element is present in the second list, remove it from the first list. After the stream operations, collect the items to a new list.

List<String> listOfAdditionalItems = listOne.stream()
    .filter(item -> !listTwo.contains(item))
    .toList();

Assertions.assertTrue(CollectionUtils.isEqualCollection(List.of("c", "d"), listOfAdditionalItems));

2.3. Using CollectionUtils.removeAll()

The CollectionUtils.removeAll(list1, list2) returns a collection containing all the elements in list1 that are not in list2. The CollectionUtils class is part of Apache commons-collection4 library.

List<String> listOfAdditionalItems = (List<String>) CollectionUtils.removeAll(listOne, listTwo);

Assertions.assertTrue(CollectionUtils.isEqualCollection(List.of("c", "d"), listOfAdditionalItems));

3. Compare Two Lists – Find Missing Items

To get the missing elements in list 1, which are present in list 2, we can reverse the solutions in the previous section.

The Solution using plain Java is:

ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "e", "f"));

//missing items in listOne
listTwo.removeAll(listOne);

System.out.println(listTwo);  //[e, f]

The solution using the stream API is as follows:

List<String> listOfMissingItems = listTwo.stream()
    .filter(item -> !listOne.contains(item))
    .toList();

Assertions.assertTrue(CollectionUtils.isEqualCollection(List.of("e", "f"), listOfMissingItems));

Similarly, use the CollectionUtils.removeAll() with the list ordered reversed.

List<String> listOfMissingItems = (List<String>) CollectionUtils.removeAll(listTwo, listOne);

Assertions.assertTrue(CollectionUtils.isEqualCollection(List.of("e", "f"), listOfMissingItems));

4. Compare Two Lists – Find Common Items

To find common elements in two arraylists, use List.retainAll() method. This method retains only the elements in this list that are contained in the specified arraylist passed as method argument.

ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "e", "f"));

//common items in listOne and listTwo
listOne.retainAll(listTwo);     //[a, b]

We can use the Stream API to find all the common items as follows:

List<String> listOfCommonItems = listOne.stream()
    .filter(item -> listTwo.contains(item))
    .toList();

Assertions.assertTrue(CollectionUtils.isEqualCollection(List.of("a", "b"), listOfCommonItems));

Also, the CollectionUtils.intersection() method can be used that returns the common elements in two lists.

List<String> listOfCommonItems = (List<String>) CollectionUtils.intersection(listTwo, listOne);

Assertions.assertTrue(CollectionUtils.isEqualCollection(List.of("a", "b"), listOfCommonItems));

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Sourcecode on Github

Comments

Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode