Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two ArrayList<List<String>>

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.

like image 374
Programming Student Avatar asked Dec 04 '22 00:12

Programming Student


2 Answers

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

like image 115
cybye Avatar answered Dec 21 '22 23:12

cybye


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.

like image 38
Jayamohan Avatar answered Dec 21 '22 21:12

Jayamohan