Check If Two Lists are Equal in Java

1. Introduction

In this short article we’ll focus on the common problem of testing if two List instances contain the same elements in exactly the same order.

List is an ordered data structure so the order of elements matters by design.

have a look at an excerpt from the List#equals Java documentation:

… two lists are defined to be equal if they contain the same elements in the same order.

This definition ensures that the equals method works properly across different implementations of the List interface.

We can use this knowledge when writing assertions.

In the following code snippets, we will be using the following lists as example inputs:

List<String> list1 = Arrays.asList("1", "2", "3", "4");
List<String> list2 = Arrays.asList("1", "2", "3", "4");
List<String> list3 = Arrays.asList("1", "2", "4", "3");

2. JUnit

In a pure JUnit test, the following assertions will be true:

@Test
public void whenTestingForEquality_ShouldBeEqual() throws Exception {
    Assert.assertEquals(list1, list2);
    Assert.assertNotSame(list1, list2);
    Assert.assertNotEquals(list1, list3);
}

3. TestNG

When using TestNG’s assertions they will look very similarly to JUnit’s assertions, but it’s important to notice that the Assert class comes from a different package:

@Test
public void whenTestingForEquality_ShouldBeEqual() throws Exception {
    Assert.assertEquals(list1, list2);
    Assert.assertNotSame(list1, list2);
    Assert.assertNotEquals(list1, list3);
}

4. AssertJ

If you like to use AssertJ, it’s assertions will look as follows:

@Test
public void whenTestingForEquality_ShouldBeEqual() throws Exception {
    assertThat(list1)
      .isEqualTo(list2)
      .isNotEqualTo(list3);

    assertThat(list1.equals(list2)).isTrue();
    assertThat(list1.equals(list3)).isFalse();
}

5. Conclusion

In this article, we have explored how to test if two List instances contain the same elements in the same order. The most important part of this problem was the proper understanding of how the List data structure is designed to work.

All code examples can be found on GitHub.

Related posts:

Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Chuyển đổi giữa các kiểu dữ liệu trong Java
Bootstrap a Web Application with Spring 5
Từ khóa static và final trong java
New Stream Collectors in Java 9
An Intro to Spring Cloud Task
Calling Stored Procedures from Spring Data JPA Repositories
LIKE Queries in Spring JPA Repositories
Truyền giá trị và tham chiếu trong java
Giới thiệu Google Guice – Dependency injection (DI) framework
Java Program to Represent Graph Using Adjacency Matrix
Java Program to Implement Expression Tree
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Implement EnumMap API
Giới thiệu JDBC Connection Pool
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Custom Thread Pools In Java 8 Parallel Streams
The Spring @Controller and @RestController Annotations
Java Program to Implement Hash Tables Chaining with Binary Trees
Guide to Guava Multimap
Exception Handling in Java
Encode/Decode to/from Base64
Spring Cloud – Bootstrapping
Introduction to Spring Data JPA
Java Program to Implement Weight Balanced Tree
Java Program to Find the Longest Path in a DAG
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Simple Single Sign-On with Spring Security OAuth2
Guide to java.util.concurrent.Future
Lập trình đa luồng với CompletableFuture trong Java 8
Hướng dẫn Java Design Pattern – Iterator
Java Program to Implement Merge Sort on n Numbers Without tail-recursion