mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
Support Collection.removeIf (#474)
This commit is contained in:
parent
d76eeb9be3
commit
a97bda3c1a
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user