79

How do I check if an ArrayList contains all of the objects in another ArrayList? I am looking (if it exists) for something along the lines of:

//INCORRECT EXAMPLE:
if(one.contains(two))
{
    return true;
}
else
{
    return false;
}

For example:

ArrayList one = {1, 2, 3, 4, 5}

ArrayList two = {1, 2, 3} --> True
ArrayList two = {} --> True
ArrayList two = {1, 2, 3, 4, 5} --> True
ArrayList two = {1, 5, 2} --> True
ArrayList two = {1, 7, 4} --> False
ArrayList two = {0, 1, 3} --> False
ArrayList two = {4, 5, 6} --> False
ArrayList two = {7, 8, 9} --> False
3
  • in that case you want containsAll (already an answer) Commented Jan 24, 2013 at 22:08
  • 3
    And you can reduce your if statement to one line: return one.containsAll(two); Commented Jan 25, 2013 at 0:40
  • Related: meta.stackoverflow.com/q/382499/1889720 Commented Apr 8, 2019 at 12:55

9 Answers 9

137

There is a method called containsAll declared in the java.util.Collection interface. In your setting one.containsAll(two) gives the desired answer.

Sign up to request clarification or add additional context in comments.

Comments

16

Per the List interface:

myList.containsAll(...);

Comments

11

Take a look at containsAll(Collection<?> c) method from List interface. I think it is what you are looking for.

Comments

9

Here is another example use of containsAll() that I have used for asserting that two arrays are equal in JUnit testing:

List<String> expected = new ArrayList<String>();
expected.add("this");
expected.add("that");
expected.add("another");

List<String> actual = new ArrayListString();
actual.add("another");
actual.add("that");
actual.add("this");

Assert.assertTrue("The lists do not match!", expected.containsAll(actual));

Comments

5

Your code in the example doesn't make sense, but here's an example anyway.

ArrayList<Integer> one, two;
//initialize
boolean good = true;
for (int i = 0; i < two.size(); i ++) {
    if (!(one.contains(two.get(i))) {
        good = false;
        break;
    }
}

It simply loops through all of two's elements and checks to see if they are in one.

Then the boolean good contains the value you want.

See ArrayList#contains.

EDIT: oh wow, I totally forgot containsAll. Oh well, this is an alternate way to do it if you really want to understand it.

3 Comments

Once it goes 'false' you should 'break'. No need to keep checking the rest.
Alternatively (instead of break) you could do: for (int i = 0; i < two.size() && good; i ++)
If you know that 'one' is ordered from small to large you could do a binary search in real code, but that would only help, timewise, if you expected 'one' to be pretty big.
5

You can use containsAll method of the list to do the check. However, this is a linear operation. If the list is large, you should convert it to HashSet first, and then perform containsAll:

HashSet tmp = new HashSet(one);
if (tmp.containsAll(two)) {
    ...
}

If the length of one is N and the length of two is M, this solution has time complexity of O(M+N); the "plain" containsAll has the complexity of O(M*N), which may be significantly worse.

Comments

4

This can also be done using streams in Java

    List<String> employeeList = Arrays.asList("Marc","john");
    List<String> masterEmployeeList = Arrays.asList("Marc", "Stacy", "john");
    System.out.println(employeeList.stream().allMatch(masterEmployeeList::contains));

Comments

-1

java.util.Collections.indexOfSubList(arrayListIS, arrayListIF)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2

isEqualCollection() method declared in the org.apache.commons.collections.CollectionUtils gives you the collections are same or not.

if (CollectionUtils.isEqualCollection(collectionA,collectionB)) { do smt... }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.