-5

Android 5.0, Java 1.7

I has 2 lists. I need to compare only content of this lists. The order of the elements does not matter. Example:

[1,2,3,4,5] , [1,2,3,4,5] -> return true

[1,2,3,4] , [1,2,3,4,5] -> return false

[1,3,5,4,2] , [1,2,3,4,5] -> return true
Alexei
  • 14,350
  • 37
  • 121
  • 240
  • 10
    by writing the code that does that, but we're not a handing out code service. – Stultuske Nov 22 '18 at 11:03
  • 1
    You an try converting list to SET and then compare them by Collection comparator – ahuja007 Nov 22 '18 at 11:05
  • 3
    duplicate https://stackoverflow.com/questions/13501142/java-arraylist-how-can-i-tell-if-two-lists-are-equal-order-not-mattering – Aiko West Nov 22 '18 at 11:06
  • 2
    Possible duplicate of [Java ArrayList - how can I tell if two lists are equal, order not mattering?](https://stackoverflow.com/questions/13501142/java-arraylist-how-can-i-tell-if-two-lists-are-equal-order-not-mattering) – Andrii Omelchenko Nov 22 '18 at 11:07

1 Answers1

2

A simple method would be to use containsAll method:

if(listOne.containsAll(listTwo) && listTwo.containsAll(listOne)) {
    System.out.println("They are equal");
}
Schidu Luca
  • 3,897
  • 1
  • 12
  • 27