Support Collection.removeIf (#474)

This commit is contained in:
Colorize 2020-02-17 09:23:24 +01:00 committed by GitHub
parent d76eeb9be3
commit a97bda3c1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -16,6 +16,7 @@
package org.teavm.classlib.java.util; package org.teavm.classlib.java.util;
import java.util.Spliterator; import java.util.Spliterator;
import java.util.function.Predicate;
import org.teavm.classlib.java.lang.TIterable; import org.teavm.classlib.java.lang.TIterable;
import org.teavm.classlib.java.util.stream.TStream; import org.teavm.classlib.java.util.stream.TStream;
import org.teavm.classlib.java.util.stream.impl.TSpliteratorOverCollection; import org.teavm.classlib.java.util.stream.impl.TSpliteratorOverCollection;
@ -55,4 +56,21 @@ public interface TCollection<E> extends TIterable<E> {
default TStream<E> stream() { default TStream<E> stream() {
return new TStreamOverSpliterator<>((Spliterator<E>) spliterator()); return new TStreamOverSpliterator<>((Spliterator<E>) spliterator());
} }
default boolean removeIf(Predicate<? super E> filter) {
TIterator<E> 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;
}
} }

View File

@ -159,4 +159,19 @@ public class ArrayListTest {
// OK // OK
} }
} }
@Test
public void removeIf() {
List<String> 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));
}
} }