Simple way to find if two different lists contain exactly the same elements?
If you're using (or are happy to use) Apache Commons Collections, you can use CollectionUtils.isEqualCollection which "returns true iff the given Collections contain exactly the same elements with exactly the same cardinalities."
If you care about order, then just use the equals method:
list1.equals(list2)
From the javadoc:
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, 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.
If you want to check independent of order, you could copy all of the elements to Sets and use equals on the resulting Sets:
public static <T> boolean listEqualsIgnoreOrder(List<T> list1, List<T> list2) {
return new HashSet<>(list1).equals(new HashSet<>(list2));
}
A limitation of this approach is that it not only ignores order, but also frequency of duplicate elements. For example, if list1
was ["A", "B", "A"] and list2
was ["A", "B", "B"] the Set
approach would consider them to be equal.
If you need to be insensitive to order but sensitive to the frequency of duplicates you can either:
- sort both lists (or copies) before comparing them, as done in this answer to another question
- or copy all elements to a Multiset
I posted a bunch of stuff in comments I think it warrants its own answer.
As everyone says here, using equals() depends on the order. If you don't care about order, you have 3 options.
Option 1
Use containsAll()
. This option is not ideal, in my opinion, because it offers worst case performance, O(n^2).
Option 2
There are two variations to this:
2a) If you don't care about maintaining the order ofyour lists... use Collections.sort()
on both list. Then use the equals()
. This is O(nlogn), because you do two sorts, and then an O(n) comparison.
2b) If you need to maintain the lists' order, you can copy both lists first. THEN you can use solution 2a on both the copied lists. However this might be unattractive if copying is very expensive.
This leads to:
Option 3
If your requirements are the same as part 2b, but copying is too expensive. You can use a TreeSet to do the sorting for you. Dump each list into its own TreeSet. It will be sorted in the set, and the original lists will remain intact. Then perform an equals()
comparison on both TreeSet
s. The TreeSets
s can be built in O(nlogn) time, and the equals()
is O(n).
Take your pick :-).
EDIT: I almost forgot the same caveat that Laurence Gonsalves points out. The TreeSet implementation will eliminate duplicates. If you care about duplicates, you will need some sort of sorted multiset.
Very late to the party but wanted to add this null safe check:
Objects.equals(list1, list2)