Cost of queries on hash sets don't depend on the size of the set. setA.retainAll(setB)
is iteration through setA
with queries on setB
(see implementation of AbstractCollection.retainAll()
). Overall cost of this operation linearly depends on the size of setA
. Therefore you should always iterate through the least set:
small.retainAll(medium);
small.retainAll(large);
Benchmark by Richard Tingle proven this.
EDIT Ah, Richard Tingle is the author of the question too :)
If you have exactly three sets and performance really matters, try to find intersection during the single iteration:
Iterator<E> it = small.iterator();
while (it.hasNext()) {
E e = it.next();
if (!medium.contains(e) || !large.contains(e))
it.remove();
}
Since Java 8:
small.removeIf(e -> !medium.contains(e) || !large.contains(e));