23

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

einpoklum
  • 102,731
  • 48
  • 279
  • 553
troyal
  • 2,439
  • 6
  • 24
  • 28

2 Answers2

45

Use Collection.containsAll():

boolean isSubset = listA.containsAll(listB);
Dan Lew
  • 83,807
  • 30
  • 180
  • 174
  • 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
  • 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
  • 86,748
  • 48
  • 217
  • 319