Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove data from list while iterating kotlin

Tags:

android

kotlin

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 
like image 750
Anuj Mody Avatar asked Feb 02 '18 06:02

Anuj Mody


People also ask

How do you remove an element from a Hashmap in Kotlin?

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.


1 Answers

use removeAll

pushList?.removeAll {  TimeUnit.MILLISECONDS.toMinutes(       System.currentTimeMillis() - it.date) > THRESHOLD } 
like image 162
Mustafa Güven Avatar answered Sep 17 '22 21:09

Mustafa Güven