45

I am new to kotlin programming. What I want is that I want to remove a particular data from a list while iterating through it, but when I am doing that my app is crashing.

for ((pos, i) in listTotal!!.withIndex()) {

            if (pos != 0 && pos != listTotal!!.size - 1) {

                if (paymentsAndTagsModel.tagName == i.header) {
                    //listTotal!!.removeAt(pos)
                    listTotal!!.remove(i)
                }



            }
        }

OR

 for ((pos,i) in listTotal!!.listIterator().withIndex()){
            if (i.header == paymentsAndTagsModel.tagName){
                listTotal!!.listIterator(pos).remove()
            }

        }

The exception which I am getting

java.lang.IllegalStateException

9 Answers 9

39
val numbers = mutableListOf(1,2,3,4,5,6)
val numberIterator = numbers.iterator()
while (numberIterator.hasNext()) {
    val integer = numberIterator.next()
    if (integer < 3) {
        numberIterator.remove()
    }
}
1
  • 1
    This should be the accepted answer as the Kotlin docs clearly state the following: "For iterating mutable collections, there is MutableIterator that extends Iterator with the element removal function remove(). So, you can remove elements from a collection while iterating it."
    – Johann
    Jun 19, 2020 at 16:41
37

use removeAll

pushList?.removeAll {  TimeUnit.MILLISECONDS.toMinutes(
      System.currentTimeMillis() - it.date) > THRESHOLD }
7
  • 1
    If you check the code, this is a very effective and quick way to filter a MutableList in-place! .retainAll() is very similar, but the predicate is negated. Aug 24, 2020 at 15:34
  • What's the difference between removeAll and removeIf ? It seems both do the same thing, no? May 6, 2021 at 10:54
  • @androiddeveloper removeAll is in kotlin.collections.MutableCollections and removeIf is in java.util.Collections
    – Archmede
    Jun 22, 2021 at 15:28
  • @Archmede But do they do the same thing? Jun 22, 2021 at 16:23
  • @androiddeveloper the javadocs have very similar descriptions
    – Archmede
    Jun 22, 2021 at 19:26
24

It's forbidden to modify a collection through its interface while iterating over it. The only way to mutate the collection contents is to use Iterator.remove.

However using Iterators can be unwieldy and in vast majority of cases it's better to treat the collections as immutable which Kotlin encourages. You can use a filter to create a new collections like so:

listTotal = listTotal.filterIndexed { ix, element ->
    ix != 0 && ix != listTotal.lastIndex && element.header == paymentsAndTagsModel.tagName
}
3
  • Ok thanks, Sir I used your solution but the thing is I have to notify my adapter for data change but it's not working
    – Anuj Mody
    Feb 2, 2018 at 7:38
  • @AnujMody Please provide more details about what adapter you are using. Have you called notifyDataSetChanged ?
    – miensol
    Feb 2, 2018 at 7:42
  • There is a much better answer from @murgupluoglu now.
    – Khantahr
    Dec 30, 2021 at 20:18
23

The answer by miensol seems perfect.

However, I don't understand the context for using the withIndex function or filteredIndex. You can use the filter function just by itself.

You don't need access to the index the list is at, if you're using lists.

Also, I'd strongly recommend working with a data class if you already aren't. Your code would look something like this

Data Class

data class Event(
        var eventCode : String,
        var header : String
)

Filtering Logic

fun main(args:Array<String>){

    val eventList : MutableList<Event> = mutableListOf(
            Event(eventCode = "123",header = "One"),
            Event(eventCode = "456",header = "Two"),
            Event(eventCode = "789",header = "Three")
    )


    val filteredList = eventList.filter { !it.header.equals("Two") }

}
1
  • 1
    Thanks alot! list = list.filter { !(it.ID== ID) }.toMutableList() Sep 4, 2019 at 10:31
9

The following code works for me:

val iterator = listTotal.iterator()
for(i in iterator){
    if(i.haer== paymentsAndTagsModel.tagName){
        iterator.remove()
    }
}

You can also read this article.

1

People didn't break iteration in previous posts dont know why. It can be simple but also with extensions and also for Map:

fun <T> MutableCollection<T>.removeFirst(filter: (T) -> Boolean) =
    iterator().removeIf(filter)

fun <K, V> MutableMap<K, V>.removeFirst(filter: (K, V) -> Boolean) =
    iterator().removeIf { filter(it.key, it.value) }

fun <T> MutableIterator<T>.removeFirst(filter: (T) -> Boolean): Boolean {
    for (item in this) if (filter.invoke(item)) {
        remove()
        return true
    }
    return false
}
0

Use a while loop, here is the kotlin extension function:

fun <E> MutableList<E>.removeIfMatch(isMatchConsumer: (existingItem: E) -> Boolean) {
    var index = 0
    var lastIndex = this.size -1

    while(index <= lastIndex && lastIndex >= 0){
        when {
            isMatchConsumer.invoke(this[index]) -> {
                this.removeAt(index)
                lastIndex-- // max is decreased by 1
            }
            else -> index++ // only increment if we do not remove
        }
    }
}
5
  • I can't find removeIfMatch. Instead I see 2 functions that seem like they do the same thing: removeAll and removeIf . How come? May 6, 2021 at 10:58
  • @androiddeveloper Make sure you're using a MutableList and not a List
    – Archmede
    Jun 22, 2021 at 3:34
  • @Archmede Hm it's of ArrayList. Important? Jun 22, 2021 at 17:23
  • @androiddeveloper ArrayList implements MutableList so it doesn't matter
    – Archmede
    Jun 22, 2021 at 19:21
  • @Archmede So it's the same? Jun 22, 2021 at 21:34
0

Typically you can use:

yourMutableCollection.removeIf { someLogic == true }

However, I'm working with an Android app that must support APIs older than 24. In this case removeIf can't be used.

Here's a solution that is nearly identical to that implemented in Kotlin Collections that doesn't rely on Predicate.test - which is why API 24+ is required in the first place

 //This function is in Kotlin Collections but only for Android API 24+
fun <E> MutableCollection<E>.removeIff(filter: (E) -> Boolean): Boolean {
    var removed = false
    val iterator: MutableIterator<E> = this.iterator()
    while (iterator.hasNext()) {
        val value = iterator.next()
        if (filter.invoke(value)) {
            iterator.remove()
            removed = true
        }
    }
    return removed
}
0

Another solution that will suit small collections. For example set of listeners in some controller.

inline fun <T> MutableCollection<T>.forEachSafe(action: (T) -> Unit) {
    val listCopy = ArrayList<T>(this)
    for (element: T in listCopy) {
        if (this.contains(element)) {
            action(element)
        }
    }
}

It makes sure that elements of collection can be removed safely even from outside code.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.