What is the cost of ContainsAll in Java?
arrays, interface, java, list
Solution
- first, it iterates each element of the supplied collection
- then it iterates all elements of the list and compares the current element with them using `.equals(..)` (Note: this is about lists, as you specified in the question. Other collections behave differently)
So it's O(n*m), where n and m are the sizes of both collections.
public boolean containsAll(Collection<?> c) {
Iterator<?> e = c.iterator();
while (e.hasNext())
if (!contains(e.next()))
return false;
return true;
}
Problem
I discovered `containsAll()` (a `List` interface method) during some coding today, and it looks pretty slick. Does anyone know how much this costs in terms of performance/iterations? The documentation didn't offer much in terms of that.