Check if two lists are equal in Java
This post will discuss how to check if two lists are equal in Java. The List may be a List of primitive types or a List of Objects. Two lists are defined to be equal if they contain exactly the same elements, in the same order.
1. Using List.equals() method
A simple solution to compare two lists of primitive types for equality is using the List.equals() method. It returns true if both lists have the same size, and all corresponding pairs of elements in both lists are equal.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.List; public class Main { public static boolean isEqual(List<Integer> x, List<Integer> y) { if (x == null) { return y == null; } return x.equals(y); } public static void main(String[] args) { List<Integer> x = List.of(1, 2, 3, 4, 5); List<Integer> y = List.of(1, 2, 3, 4, 5); boolean isEqual = isEqual(x, y); if (isEqual) { System.out.println("Both lists are equal"); } else { System.out.println("Both lists are not equal"); } } } |
Output:
Both lists are equal
2. Using Objects.equals() method
Alternatively, we can use the Objects.equals() method for comparing two lists of primitive types in Java. It returns true if the arguments are equal to each other, and false otherwise. The advantage of using this method is that it is null-safe and there’s no need to explicitly handle nulls.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.List; import java.util.Objects; public class Main { public static void main(String[] args) { List<Integer> x = List.of(1, 2, 3, 4, 5); List<Integer> y = List.of(1, 2, 3, 4, 5); boolean isEqual = Objects.equals(x, y); // null-safe if (isEqual) { System.out.println("Both lists are equal"); } else { System.out.println("Both lists are not equal"); } } } |
Output:
Both lists are equal
It is worth noting that Objects.equals() calls List.equals() method internally.
|
1 2 3 |
public static boolean equals(Object a, Object b) { return (a == b) || (a != null && a.equals(b)); } |
You can also use the Objects.deepEquals() method that returns true if the arguments are “deeply” equal to each other and false otherwise.
3. Check equality in List of objects
To compare two lists of objects in Java, you can use either List.equals(…) or Objects.equals(…) methods, as discussed above. However, you would also need to override equals and hashCode methods for that object in Java. The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import java.util.*; class Person { String name; int age; Person(String name, int age) { this.name = name; this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } @Override public int hashCode() { return Objects.hash(name, age); } } public class Main { public static void main(String[] args) { List<Person> presidents1 = List.of( new Person("Obama", 60), new Person("Trump", 75), new Person("Biden", 79)); List<Person> presidents2 = List.of( new Person("Obama", 60), new Person("Trump", 75), new Person("Biden", 79)); boolean isEqual = Objects.equals(presidents1, presidents2); if (isEqual) { System.out.println("Both lists are equal"); } else { System.out.println("Both lists are not equal"); } } } |
Output:
Both lists are equal
That’s all about checking if two lists are equal in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)