diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TCollection.java b/classlib/src/main/java/org/teavm/classlib/java/util/TCollection.java index 150284a10..c4600a3b0 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TCollection.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TCollection.java @@ -16,6 +16,7 @@ package org.teavm.classlib.java.util; import java.util.Spliterator; +import java.util.function.Predicate; import org.teavm.classlib.java.lang.TIterable; import org.teavm.classlib.java.util.stream.TStream; import org.teavm.classlib.java.util.stream.impl.TSpliteratorOverCollection; @@ -55,4 +56,21 @@ public interface TCollection extends TIterable { default TStream stream() { return new TStreamOverSpliterator<>((Spliterator) spliterator()); } + + default boolean removeIf(Predicate filter) { + TIterator iterator = iterator(); + boolean removed = false; + + while (iterator.hasNext()) { + E element = iterator.next(); + boolean match = filter.test(element); + + if (match) { + iterator.remove(); + removed = true; + } + } + + return removed; + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java b/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java index 6b63c80f1..d686601a7 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java @@ -159,4 +159,19 @@ public class ArrayListTest { // OK } } + + @Test + public void removeIf() { + List list = new ArrayList<>(); + list.add("A1"); + list.add("A2"); + list.add("B1"); + list.add("B2"); + + list.removeIf(e -> e.endsWith("2")); + + assertEquals(2, list.size()); + assertEquals("A1", list.get(0)); + assertEquals("B1", list.get(1)); + } }