1

I have two arraylist

ArrayList<String> a1
ArrayList<String> a2

I want to check if all the elements of a1 are present in a2. this is what I was trying which I got from a question on SO

if (Arrays.asList(a2).containsAll(Arrays.asList(a1)))
{a2ContainsA1=true;}

which is not giving consistent results...is there any other way of doing this?

10
  • 4
    What should happen if you have list 1 containing a, a, b and list 2 containing a, b, b? This makes quite a difference.
    – fge
    Jan 7, 2013 at 19:17
  • 6
    In what way are the results inconsistent? Jan 7, 2013 at 19:19
  • 1
    Could you include a complete runnable example with an explanation of what you expect to happen and what actually happens?
    – NPE
    Jan 7, 2013 at 19:21
  • 1
    Arrays.asList(a2) should return a list with one element. Jan 7, 2013 at 19:26
  • 1
    Have you investigated that the objects stored in the arrays correctly implement equals() ? Jan 7, 2013 at 19:26

8 Answers 8

7

Not sure if I understand your question correctly, why are you using Arrays.asList() ?

Tried this:

    ArrayList<String> a1 = new ArrayList<String>();
    ArrayList<String> a2 = new ArrayList<String>();

    a1.add("a");
    a1.add("b");
    a2.add("b");
    a2.add("a");

    System.out.println(a2.containsAll(a1));

And it outputs true Adding an additional element to a1 will fail it (as expected):

     ArrayList<String> a1 = new ArrayList<String>();
    ArrayList<String> a2 = new ArrayList<String>();

    a1.add("a");
    a1.add("b");
    a1.add("c");
    a2.add("b");
    a2.add("a");

    System.out.println(a2.containsAll(a1));

And it outputs false

2
  • What about multiplicity? To me this answer does not seem complete. Jan 7, 2013 at 19:27
  • @AlessandroSantini: the question doesn't describe the problem fully. This question is probably the answer, and to me, there's no point in providing every possible solution to a non-clear problem. The OP should define his requirements clearly.
    – JB Nizet
    Jan 7, 2013 at 19:36
2

I think it would be easier to convert the two ArrayLists to Set objects then use set operations to determine if one is a subset of another. This will also satisfy @fge's comment about duplicity:

ArrayList<String> a1 = ...;
ArrayList<String> a2 = ...;

public boolean isSubset(ArrayList<String> a1, ArrayList<String> a2) {
   Set<String> s1 = new HashSet(a1);
   Set<String> s2 = new HashSet(a2);

   return s1.containsAll(s2);
}
7
  • You could just s1.removeAll(a2) and test if s1 is empty afterwards. And I do suspect this is what the OP means, but he didn't tell, so...
    – fge
    Jan 7, 2013 at 19:23
  • @fge Yeah, there are several ways that you could do this. I was just going to present one, but I guess it would be good to mention others. Jan 7, 2013 at 19:23
  • It is not clear whether duplicates are allowed and whether that matters. Jan 7, 2013 at 19:25
  • That is not correct. If the two arrays are supposed to hold duplicates, A2 is a subset of A1 if it contains only elements of A1 and with the same multiplicity. Jan 7, 2013 at 19:25
  • Peter Lawrey - if they are not supposed to hold duplicates, he is using the wrong data structure (should be using Sets) Jan 7, 2013 at 19:25
2

You already have your lists a1 and a2 here, and what you want is to tell whether all unique elements in a1 are also in a2. Therefore you just need to test for:

a1.containsAll(a2)

Arrays.asList() will build a list out of all elements you give as arguments (even if that "all elements" is only one element). In fact, your current code tries to see whether a single-element list which contains the list object a1 contains all elements in another single-element list containing the list object a2. And this is true if and only if lists a1 and a2 are equal, that is, if both of these lists contain the same elements at the same index, as per the List contract.

Which is certainly not what you were looking for!

1
  • last line of your answer is true and I have observed while using asList() method... +1 for clearing the concept Jan 8, 2013 at 15:55
1

you can loop through one of the arrays and do contains on each element therein

boolean a2ContainsA1 = true;
for(String str: a2){
    if(!a1.contains(str)){
      a2ContainsA1 = false;
}
3
  • 2
    This should be fully equivalent to what the OP already tried. Jan 7, 2013 at 19:20
  • Yes, but complexity is n*m ! Jan 7, 2013 at 19:27
  • @vikasdevde please answer my question in the comments right behind your question. If the answer is "yes" then you should go with HunterMcMillen's answer.
    – fge
    Jan 7, 2013 at 19:30
1

You should try (assuming you don't want to keep a1 around, or else clone it)

boolean conditionVerified = a1.removeAll(a2).isEmpty();
0

You can use method isEqualList() of ListUtils like -

ArrayList<String> a1 = new ArrayList<String>();
ArrayList<String> a2 = new ArrayList<String>();

boolean isEqual = ListUtils.isEqualList(a1,a2);
1
  • Actually, after a lof ot talkabout, it appears that the .containsAll() method of Collection was enough :p
    – fge
    Jan 7, 2013 at 19:49
0
boolean contains = true;
for(int i=0 ; i<a1.size() ; i++)
{
  if(!a2.contains(a1[i])
      contains = false;
}

Is this what you're looking for?

0

disjoint seems to be a good candidate here..

check if disjoint using !Collections.disjoint(list1, list2);

basic example at http://www.tutorialspoint.com/java/util/collections_disjoint.htm

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.