Метод retainAll
override
Удаляет все элементы этого множества, которые не являются элементами в elements.
Проверяет для каждого элемента elements , есть ли элемент в этом множестве, который ему равен (согласно this.contains), и если есть, то равный элемент в этом множестве сохраняется, а элементы, которые не равны ни одному элементу в elements, удаляются.
final characters = <String>{'A', 'B', 'C'};
characters.retainAll({'A', 'B', 'X'});
print(characters); // {A, B} Реализация
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++;
}
}
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-collection/SplayTreeSet/retainAll.html