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?
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With