Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we remove elements from a MutableList in Kotlin

I have the following code where I need to display the elements of a list in view and then remove those items from the list. I have been looking into filter vs map in kotlin but no luck finding a solution.

var mutableList: MutableList<Object> = myImmutableList.toMutableList()
for (x in mutableList.indices)
{
    val tile = row!!.getChildAt(x % 4)
    val label = tile.findViewById(android.R.id.text1) as TextView
    label.text = mutableList[x].name
    val icon = tile.findViewById(android.R.id.icon) as ImageView
    picasso.load(mutableList[x].icon).into(icon)
}
like image 266
MR Mido Avatar asked Jan 21 '17 18:01

MR Mido


People also ask

How do I remove an element from an array in Kotlin?

How do you remove the first element from an array in Kotlin? Using removeAt() function. The removeAt() function removes an element at the specified index from a mutable list. Using clear() function.

How do you remove if in Kotlin?

Using removeIf() function The simplest solution is to directly call the removeIf() function, which removes all elements from the list that satisfies the given predicate. That's all about conditionally remove elements from a list in Kotlin.

How do I remove a subList from a list in Kotlin?

You can pass a sublist returned by subList() function to removeAll() function to remove them. The slice() function can also be used instead of subList() .


2 Answers

Since you are iterating through the whole list, simplest way would be to call clear method of MutableList after you process all items.

mutableList.clear()

Other option could be method remove to remove given element or removeAt to remove element at given index. Both are again methods of MutableList class. In practice it would look like this.

val list = listOf("a", "b", "c")
val mutableList = list.toMutableList()
for (i in list.indices) {
    println(i)
    println(list[i])
    mutableList.removeAt(0)
}
like image 90
Januson Avatar answered Sep 24 '22 09:09

Januson


Is there a reason you can't just map and filter the initial immutable collection? You are already making a copy when you call "List#toMutableList()", so I don't quite see what you're trying to accomplish by avoiding it.

val unprocessedItems = myImmutableList.asSequence().mapIndexed { index, item ->
    // If this item's position is a multiple of four, we can process it
    // The let extension method allows us to run a block and return a value
    // We can use this and null-safe access + the elvis operator to map our values
    row?.getChildAt(index % 4)?.let {
        val label = it.findViewById(android.R.id.text1) as TextView

        label.text = item.name
        val icon = it.findViewById(android.R.id.icon) as ImageView

        picasso.load(item.icon).into(icon)

        // Since it's processed, let's remove it from the list
        null
    } ?: item // If we weren't able to process it, leave it in the list
}.filterNotNull().toList()

Again, not quite sure what you're going for with this. I think there might be a better approach given more detail.

like image 37
mbStavola Avatar answered Sep 24 '22 09:09

mbStavola