collect method

  1. @Possible({ConcurrentModificationError})
void collect(
  1. Consume<E> consume
)

Moves the set's elements that satisfy the predicate to consume.

Contract

A ConcurrentModificationError is thrown if consume modifies the set.

Example

final foo = {1, 2, 3, 4, 5};
final bar = [];

foo.move(where: (e) => e.isEven).collect(bar.add);

print(foo); // {1, 3, 5}
print(bar); // [2, 4]

Implementation

@Possible({ConcurrentModificationError})
void collect(Consume<E> consume) {
  final removed = <E>[];
  for (final element in _set) {
    if (_predicate(element)) {
      consume(element);
      removed.add(element);
    }
  }

  _set.removeAll(removed);
}