Use cases of removeall and removeif

Java's removeIf() is there since Java 1.8.

Kotlin started at 2011 (wikipedia). Java 1.8 appeared in 2014.

I'm not sure when the Kotlin's removeAll(predicate) was specified and implemented, however it probably predates Java's removeIf().


There is one more important difference:

Calling removeIf on a CopyOnWriteArrayList is thread-safe, but removeAll is not.

Looking at the code, removeIf has a custom implementation for CopyOnWriteArrayList, but removeAll iterates over the indices and will end up throwing ArrayIndexOutOfBoundsException or even worse, removing the wrong element, if called concurrently.

Tags:

Java

Kotlin