简体   繁体   中英

ContainsAll List Java

     List<String> a = new ArrayList<String>();
     List<String> b = new ArrayList<String>();

     a.add("apple");
     a.add("orange");

     System.out.println(a.containsAll(b));

The above program prints a True. Dont understand why is it printing True?

Because B is empty. A contains everything in B .

Because b is empty. Therefore there is nothing in b that is not in a .

It's a matter of logic: does A contain all the elements inside B?

This can be seen as for each element in B, does this element belong to A too?

You can understand that the condition is true, since B is empty, there is no element to check: for each element in B, so for no element.

List.ContainsAll will return true if the list contains all of the elements within the target. Because B is empty A contains all the same elements as B.

Obviously a typo. b.add("orange") is what was meant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM