Hi I checked Params of Contains methods and ContainsAll method .
boolean containsAll(Collection<?> c);
boolean contains(Object o);
It seems both will accept Object . Is there any difference among two.
Hi I checked Params of Contains methods and ContainsAll method .
boolean containsAll(Collection<?> c);
boolean contains(Object o);
It seems both will accept Object . Is there any difference among two.
No, they do not both accept Object. Let us assume you have an Collection x:
The first method (x.containsAll(c)
) accepts a Collection c
and will return true if ALL elements in that Collection are also contained in x.
The second one (x.contains(o)
) accepts an Object o
and will return true if that Object is contained in x.
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).
Also containsAll may accept Object because Object is the superclass of every class in Java. However, if the Object you pass is not an instance of Collection, you'll have a ClassCastException at runtime.
Yes, there is a difference. containsAll
will not accept Object. It will only accept a Collections of Objects.
Method below accepts any type since all derive from Object
contains(Object o);
Method below accepts a collection of any type since wildcard ?
is used:
containsAll(Collection<?> c);