retainAll method Null safety
override
Removes all elements of this set that are not elements in elements
.
Checks for each element of elements
whether there is an element in this
set that is equal to it (according to this.contains
), and if so, the
equal element in this set is retained, and elements that are not equal
to any element in elements
are removed.
Implementation
void retainAll(Iterable<Object?> elements) {
// Build a set with the same sense of equality as this set.
SplayTreeSet<E> retainSet = SplayTreeSet<E>(_compare, _validKey);
int modificationCount = _modificationCount;
for (Object? object in elements) {
if (modificationCount != _modificationCount) {
// The iterator should not have side effects.
throw ConcurrentModificationError(this);
}
// Equivalent to this.contains(object).
if (_validKey(object) && _splay(object as E) == 0) {
retainSet.add(_root!.key);
}
}
// Take over the elements from the retained set, if it differs.
if (retainSet._count != _count) {
_root = retainSet._root;
_count = retainSet._count;
_modificationCount++;
}
}