I want to compare these two ArrayList
:
public static ArrayList<List<String>> arrayList1 = new ArrayList<List<String>>();
public static ArrayList<List<String>> arrayList2 = new ArrayList<List<String>>();
If they have the same elements it will return true, otherwise false.
Have you tried
arrayList1 .equals ( arrayList2 )
which is true, if they contain the same elements in the same order
or
new HashSet(arrayList1) .equals (new HashSet(arrayList2))
if the order does not matter
See http://www.docjar.com/html/api/java/util/AbstractList.java.html
You can try using Apache commons CollectionUtils.retainAll method : Returns a collection containing all the elements in collection1 that are also in collection2.
ArrayList commonList = CollectionUtils.retainAll(arrayList1, arrayList2);
If the size of commonList is equal to arrayList1
and arrayList2
you can say both the list are same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With