-
Enhancement
-
Resolution: Unresolved
-
P3
-
None
Add a new default method Collection.removeIf(predicate, consumer). This is similar to the existing Collection.removeIf(predicate) in that it invokes the predicate on each element of the collection and removes it if the predicate returns true. In addition, the consumer is called on every such element.
For example, to transfer matching elements from one list to another, one can do this:
var list1 = ... ;
var list2 = ... ;
list1.removeIf(this::match, list2::add);
If the consumer is null, this would be identical to the one-arg removeIf(). Existing implementations could be retrofitted by adding the consumer to all existing overrides and then providing a one-arg override that delegates to the two-arg override, passing null as the consumer.
For example, to transfer matching elements from one list to another, one can do this:
var list1 = ... ;
var list2 = ... ;
list1.removeIf(this::match, list2::add);
If the consumer is null, this would be identical to the one-arg removeIf(). Existing implementations could be retrofitted by adding the consumer to all existing overrides and then providing a one-arg override that delegates to the two-arg override, passing null as the consumer.