diego-gomez-olvera
07/17/2020, 11:18 AMpublic fun <T> MutableList<T>.removeAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, true)
private fun <T> MutableList<T>.filterInPlace(predicate: (T) -> Boolean, predicateResultToRemove: Boolean): Boolean {
if (this !is RandomAccess)
return (this as MutableIterable<T>).filterInPlace(predicate, predicateResultToRemove)
var writeIndex: Int = 0
for (readIndex in 0..lastIndex) {
val element = this[readIndex]
...
if (writeIndex != readIndex)
this[writeIndex] = element
...
}
It seems CopyOnWriteArrayList.removeAll does not copy on write but mutates in place insteadgian
07/17/2020, 3:03 PMCopyOnWriteArrayList::set copying the array before mutation?diego-gomez-olvera
07/21/2020, 7:49 AMdiego-gomez-olvera
07/21/2020, 7:49 AMjava.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at java.util.concurrent.CopyOnWriteArrayList.get(CopyOnWriteArrayList.java:385)
at java.util.concurrent.CopyOnWriteArrayList.get(CopyOnWriteArrayList.java:398)
at kotlin.collections.CollectionsKt__MutableCollectionsKt.filterInPlace$CollectionsKt__MutableCollectionsKt(MutableCollections.kt:190)
at kotlin.collections.CollectionsKt__MutableCollectionsKt.removeAll(MutableCollections.kt:177)diego-gomez-olvera
07/21/2020, 7:52 AMlastIndex should be recalculatedShivam
09/11/2025, 7:18 AMremoveAll. I'm using SynchronizedList but I don't get why it's throwing IndexOutOfBounds.
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
at java.util.Objects.checkIndex(Objects.java:385)
at java.util.ArrayList.remove(ArrayList.java:558)
at java.util.Collections$SynchronizedList.remove(Collections.java:2791)
at kotlin.collections.CollectionsKt__MutableCollectionsKt.filterInPlace$CollectionsKt__MutableCollectionsKt(CollectionsKt__MutableCollectionsKt.java:296)
at kotlin.collections.CollectionsKt__MutableCollectionsKt.removeAll(CollectionsKt__MutableCollectionsKt.java:270)