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
The standard solution is to remove a key from a Map in Kotlin is using the remove() function. When the remove() function is called upon a key k , the mapping from key k to value v is removed from the map.
use removeAll
pushList?.removeAll { TimeUnit.MILLISECONDS.toMinutes( System.currentTimeMillis() - it.date) > THRESHOLD }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With