How to check whether the elements of an ArrayList are all contained in another ArrayList
Asked Answered
A

2

26

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

Admittance answered 30/4, 2009 at 18:30 Comment(0)
I
48

Use Collection.containsAll():

boolean isSubset = listA.containsAll(listB);
Isma answered 30/4, 2009 at 18:34 Comment(5)
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)Heliogravure
Set common = new HashSet(listA); common.retainAll(listB); // now "common" contains only the common elementsIndian
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?Martino
How to check if ATLEAST ONE of the elements of a String ArrayList are contained in another String ArrayList??Passionless
As this is case sensitive, how to validate for case insensitive lists?Exergue
C
2

There is a containsAll method in all collections.

Cordey answered 30/4, 2009 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.