Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how come list.containsAll(emptylist) returns true?

Tags:

java

How is it possible that when I have a List that contains elements, and I call the containsAll method on this list with an empty list as an argument, it returns true? The empty list doesen't contain any elements so how can both lists have any elements in common?

like image 701
Maurice Avatar asked Oct 30 '17 11:10

Maurice


People also ask

What does containsAll do in Java?

The containsAll() method of Java Set is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set.

How to check if one list contains all elements of another list Java?

The containsAll() method of List interface in Java is used to check if this List contains all of the elements in the specified Collection. So basically it is used to check if a List contains a set of elements or not.

What is the difference between contains and containsAll in Java?

The difference between contains and containsAll is that contains check if 1 Object (the parameter) exists in the list while containsAll check if the list contains ALL the elements in the given collection (hence the all in the method's name).


1 Answers

From the Javadoc of List:

boolean java.util.List.containsAll(Collection c)

Returns true if this list contains all of the elements of the specified collection.

If c contains no elements, the list on which you call the method does contain all the elements of c, which is why true is returned.

like image 80
Eran Avatar answered Sep 21 '22 04:09

Eran