Java ArrayList - how can I tell if two lists are equal, order not mattering?
You could sort both lists using Collections.sort()
and then use the equals method. A slighly better solution is to first check if they are the same length before ordering, if they are not, then they are not equal, then sort, then use equals. For example if you had two lists of Strings it would be something like:
public boolean equalLists(List<String> one, List<String> two){
if (one == null && two == null){
return true;
}
if((one == null && two != null)
|| one != null && two == null
|| one.size() != two.size()){
return false;
}
//to avoid messing the order of the lists we will use a copy
//as noted in comments by A. R. S.
one = new ArrayList<String>(one);
two = new ArrayList<String>(two);
Collections.sort(one);
Collections.sort(two);
return one.equals(two);
}
Two Arraylists have the same elements but in different order, are they equal?
if (INITIAL1.size() == PL1.size() && INITIAL1.containsAll(PL1)) {
//do something
}
Simple way to find if two different lists contain exactly the same elements?
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
Why two ListObject[] not equal in Java?
These two lists have the same elements but in a different order:
List<Point[]> expectedResult = Arrays.asList(combinationPoints1, combinationPoints2, combinationPoints3, combinationPoints4);
List<Point[]> expectedResult2 = Arrays.asList(combinationPoints2, combinationPoints1, combinationPoints3, combinationPoints4);
The List.equals
method considers two lists to be equal if they contain the "same" elements in the same order. These two lists do have the same elements, but they are not in the same order. So the equals
method must return false
.
There's also a potential issue that stems from the fact that arrays don't implement equals method based on the contents: two different arrays instances with the same contents are not considered "equal" by the equals method. That doesn't come to into play here though: all of your arrays have different contents anyway.
How can I check if two ArrayList differ, I don't care what's changed
Here's a simple method that checks if 2 Array Lists contain the same values regardless their order.
//the name of the method explains it well...
public boolean isTwoArrayListsWithSameValues(ArrayList<Object> list1, ArrayList<Object> list2)
{
//null checking
if(list1==null && list2==null)
return true;
if((list1 == null && list2 != null) || (list1 != null && list2 == null))
return false;
if(list1.size()!=list2.size())
return false;
for(Object itemList1: list1)
{
if(!list2.contains(itemList1))
return false;
}
return true;
}
Simple way to find if two different lists contain exactly the same elements?
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
Check two lists for order equality after sorting elements of one list
Just use first.equals(second)
after sorting one of the lists. If both have the same number of elements and the elements are pair-wise equal, you'll get true.
This relies, of course, on the element type of the Lists to override equals
properly.
Efficient way to Compare Two List's object values without implenmenting equals()?
Try using Apache Commons isEqualCollection(Collection a, Collection b, Equator equator), you will still need to implement Equator in the same way you would write a equals override.
You could try this one too, this is the same algorithm from Apache Commons but with some performance improvements.
Related Topics
How to Enumerate All Classes in a Package and Add Them to a List
How to Display a Svg Byte Array as an Image in a Jasperreport
Should Private Helper Methods Be Static If They Can Be Static
How to Disable Jsessionid in Tomcat Servlet
How to Import Spring-Config.Xml of One Project into Spring-Config.Xml of Another Project
Why Is Javafx Is Not Included in Openjdk 8 on Ubuntu Wily (15.10)
Variable's Scope in a Switch Case
How to Test Void Method with Junit Testing Tools
Measure Execution Time for a Java Method
Selenium Many Logs (How to Remove)
Functional Style of Java 8's Optional.Ifpresent and If-Not-Present
Java Simpledateformat for Time Zone with a Colon Separator
Compiler Error When Declaring a Variable Inside If Condition and No Curly Braces