This post will discuss how to compare two lists for equality in Java, ignoring the order. 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 equal quantity each, in any order.
For example, [1, 2, 3] and [2, 1, 3] are considered equal, while [1, 2, 3] and [2, 4, 3] are not. The elements count also matters, hence, [1, 2, 3, 1] and [2, 1, 3, 2] are not treated equally. If the elements count doesnt matter, you can convert both lists to a Set and compare them using the .equals[] method of the Set interface.
1. Sorting
A simple solution is to sort both lists and then compare them using the .equals[] method of the List interface. Note that this solution is not linear, and has O[n.log[n]] time complexity. It is not suitable for large lists.
To improve efficiency, it is recommended to first check if both lists have the same size or not. Also before sorting both lists, create their copy to avoid destroying the original order of both lists. Both copying and sorting steps can be done efficiently using Streams API, as shown below.
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 | import java.util.List; import java.util.stream.Collectors; public class Main { public static boolean isEqualIgnoringOrder[List x, List y] { if [x == null] { return y == null; } if [x.size[] != y.size[]] { return true; } x = x.stream[].sorted[].collect[Collectors.toList[]]; y = y.stream[].sorted[].collect[Collectors.toList[]]; return x.equals[y]; } public static void main[String[] args] { List x = List.of[1, 2, 3]; List y = List.of[2, 3, 1]; boolean isEqual = isEqualIgnoringOrder[x, y]; if [isEqual] { System.out.println["Both lists are equal"]; } else { System.out.println["Both lists are not equal"]; } } } |
DownloadRun Code
Output:
Both lists are equal
2. Using Multiset
Another solution is to convert both lists to multiset and compare the multiset, which compares elements regardless of their order and also preserves the count of duplicate elements. The following solution demonstrates this using Guavas Multiset.
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 | import com.google.common.collect.ImmutableMultiset; import java.util.List; public class Main { public static boolean isEqualIgnoringOrder[List x, List y] { if [x == null] { return y == null; } if [x.size[] != y.size[]] { return true; } return ImmutableMultiset.copyOf[x].equals[ImmutableMultiset.copyOf[y]]; } public static void main[String[] args] { List x = List.of[1, 2, 2]; List y = List.of[1, 2, 1]; boolean isEqual = isEqualIgnoringOrder[x, y]; if [isEqual] { System.out.println["Both lists are equal"]; } else { System.out.println["Both lists are not equal"]; } } } |
Download Code
Output:
Both lists are not equal
3. Apache Commons Lang Library
Finally, Apache Commons Lang Library offers the CollectionUtils.isEqualCollection[] method, which returns true if the given Collections contain exactly the same elements with exactly the same cardinalities.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import org.apache.commons.collections4.CollectionUtils; import java.util.List; public class Main { public static void main[String[] args] { List x = List.of[1, 2, 3]; List y = List.of[2, 3, 1]; boolean isEqual = CollectionUtils.isEqualCollection[x, y]; if [isEqual] { System.out.println["Both lists are equal"]; } else { System.out.println["Both lists are not equal"]; } } } |
Download Code
Output:
Both lists are equal
4. Check equality in List of objects
To compare two lists of objects using any of the above methods, you 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 50 51 | import org.apache.commons.collections4.CollectionUtils; import java.util.*; class President { String name; int age; President[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; President person = [President] 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 x = List.of[ new President["Trump", 75], new President["Obama", 60], new President["Trump", 75]]; List y = List.of[ new President["Obama", 60], new President["Trump", 75], new President["Trump", 75]]; boolean isEqual = CollectionUtils.isEqualCollection[x, y]; if [isEqual] { System.out.println["Both lists are equal"]; } else { System.out.println["Both lists are not equal"]; } } } |
Download Code
Output:
Both lists are equal
Thats all about comparing two lists for equality in Java, ignoring the order.