replaceAll method
- @Possible({ConcurrentModificationError})
- void function(
- bool replace(
- E element
- E element
- bool replace(
Replaces elements using function
.
function
accepts a Consume used to specify an element's replacements. An element can be replaced by zero or more
elements. This function is an in-place 1:N map function.
Contract
A ConcurrentModificationError is thrown if function
directly modifies this set.
final foo = {1};
foo.replaceAll((_, __) => foo.remove(0)); // throws ConcurrentModificationError
Example
void multiplyOdd(Consume<int> add, int element) {
if (element.isOdd)
replace(element * 10);
}
{1, 2, 3, 4}.replaceAll(multiplyOdd); // {10, 30}
Implementation
@Possible({ConcurrentModificationError})
void replaceAll(void Function(bool Function(E element) replace, E element) function) {
final retained = <E>{};
final length = this.length;
for (final element in this) {
function(retained.add, element);
if (length != this.length) {
throw ConcurrentModificationError(this);
}
}
clear();
addAll(retained);
}