26

How can I easily check to see whether all the elements in one ArrayList are all elements of another ArrayList?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
troyal
  • 2,499
  • 6
  • 25
  • 28

2 Answers2

48

Use Collection.containsAll():

boolean isSubset = listA.containsAll(listB);
Dan Lew
  • 85,990
  • 32
  • 182
  • 176
  • is there anything available so that a new array is generated containing all data that's shared in listA and listB? Object[] subset = listA.shared(listB) – Someone Somewhere May 24 '11 at 23:34
  • 2
    Set common = new HashSet(listA); common.retainAll(listB); // now "common" contains only the common elements – JimN Aug 02 '11 at 02:04
  • Is there a way to check for the order of the elements as well? I tried this and it was true even though I'd changed the order of the elements. Is there a way to do what I'd like to do? – CodingInCircles Apr 02 '13 at 20:39
  • How to check if ATLEAST ONE of the elements of a String ArrayList are contained in another String ArrayList?? – Shikhar Mar 18 '19 at 04:50
  • 1
    As this is case sensitive, how to validate for case insensitive lists? – Brooklyn99 Nov 06 '19 at 23:21
2

There is a containsAll method in all collections.

Uri
  • 88,451
  • 51
  • 221
  • 321