Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:


1. Overview

Sometimes when writing unit tests, we need to make order-agnostic comparisons of lists. In this short tutorial, we’ll take a look at different examples of how we can write such unit tests.

2. Setup

As per the List#equals Java documentation, two lists are equal if they contain the same elements in the same order. Therefore we can’t merely use the equals method as we want to do order agnostic comparison.

Throughout this tutorial, we’ll use these three lists as example inputs for our tests:

List first = Arrays.asList(1, 3, 4, 6, 8);
List second = Arrays.asList(8, 1, 6, 3, 4);
List third = Arrays.asList(1, 3, 3, 6, 6);

There are different ways to make order-agnostic comparisons. Let’s take a look at them one by one.

3. Using JUnit

JUnit is a well-known framework used for unit testing in the Java ecosystem.

We can use the logic below to compare the equality of two lists using the assertTrue and assertFalse methods.

Here we check the size of both lists and check if the first list contains all elements of the second list and vice versa. Although this solution works, it’s not very readable. So now let’s look at some alternatives:

public void whenTestingForOrderAgnosticEquality_ShouldBeTrue() {
    assertTrue(first.size() == second.size() && first.containsAll(second) && second.containsAll(first));

In this first test, the size of both lists is compared before we check if the elements in both lists are the same. As both of these conditions return true, our test will pass.

Let’s now take a look at a failing test:

public void whenTestingForOrderAgnosticEquality_ShouldBeFalse() {
    assertFalse(first.size() == third.size() && first.containsAll(third) && third.containsAll(first));

Contrastingly, in this version of the test, although the size of both lists is the same, all elements don’t match.

4. Using AssertJ

AssertJ is an open-source, community-driven library for writing fluent and rich assertions in Java tests.

To use it in our maven project, let’s add the assertj-core dependency in the pom.xml file:


Let’s write a test to compare the equality of two list instances of the same element and the same size:

void whenTestingForOrderAgnosticEqualityBothList_ShouldBeEqual() {

In this example, we verify first contains all the elements of the given iterable and nothing else, in any order. The main limitation of this approach is the hasSameElementsAs method ignores duplicates.

Let’s look at this in practice to see what we mean:

void whenTestingForOrderAgnosticEqualityBothList_ShouldNotBeEqual() {
    List a = Arrays.asList("a", "a", "b", "c");
    List b = Arrays.asList("a", "b", "c");

In this test, although we have the same elements, the size of both lists is not equal, but the assertion will still be true, as it ignores the duplicates.

To overcome issues related to hasSameElementsAs(), we can use containsExactlyInAnyOrderElementsOf(), which verifies that both lists contain exactly the same elements and nothing else in any order:


The test will indeed fail as expected.

5. Using Hamcrest

If we are already using Hamcrest or want to use it for writing unit tests, here is how we can use the Matchers#containsInAnyOrder method for order-agnostic comparison.

To use Hamcrest in our maven project, let’s add the hamcrest-all dependency in pom.xml file:


Let’s look at the test:

public void whenTestingForOrderAgnosticEquality_ShouldBeEqual() {
    assertThat(first, Matchers.containsInAnyOrder(second.toArray()));

Here the method containsInAnyOrder creates an order agnostic matcher for Iterables, which does matching with examined Iterable elements. This test matches the elements of two lists, ignoring the order of elements in the list.

Thankfully this solution doesn’t suffer from the same problem as explained in the previous section, so we don’t need to compare the sizes explicitly.

6. Using Apache Commons

Another library or framework apart from JUnit, Hamcrest, or AssertJ we can use is Apache CollectionUtils. It provides utility methods for common operations that cover a wide range of use cases and helps us avoid writing boilerplate code.

To use it in our maven project, let’s add the commons-collections4 dependency in pom.xml file:


Here is a test using CollectionUtils:

public void whenTestingForOrderAgnosticEquality_ShouldBeTrueIfEqualOtherwiseFalse() {
    assertTrue(CollectionUtils.isEqualCollection(first, second));
    assertFalse(CollectionUtils.isEqualCollection(first, third));

The isEqualCollection method returns true if the given collections contain precisely the same elements with the same cardinalities. Otherwise, it returns false.

7. Conclusion

In this article, we have explored how to check the equality of two List instances where the elements of both lists are ordered differently.

All these examples can be found over on GitHub.

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:


res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.