Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove item from array list in kotlin

Tags:

kotlin

data class RideDate(var enddate: String? = null,var startdate: String? = null) 



 fun main(args: Array<String>) {
var listOfRideDate = mutableListOf<RideDate>();
val date1 = RideDate()
date1.startdate = "2018-11-05 00:00:00 +0000"
date1.enddate = "2018-11-06 23:59:59 +0000"
listOfRideDate.add(date1)

val date2 = RideDate()
date2.startdate = "2020-01-20 00:00:00 +0000"
date2.enddate = "2020-02-20 00:00:00 +0000"
listOfRideDate.add(date2)

val date3 = RideDate()
date3.startdate = "2020-03-20 00:00:00 +0000"
date3.enddate = "2020-03-20 00:00:00 +0000"
listOfRideDate.add(date3)

val date4 = RideDate()
date4.startdate = "2020-04-20 00:00:00 +0000"
date4.enddate = "2020-04-20 00:00:00 +0000"
listOfRideDate.add(date4)

val date5 = RideDate()
date5.startdate = "2020-11-20 00:00:00 +0000"
date5.enddate = "2020-11-20 00:00:00 +0000"
listOfRideDate.add(date5)


for (i in 0..listOfRideDate.size -1) {
    // we have to remove 2018-11-05 00:00:00 +0000 and 2018-11-06 23:59:59 +0000  from list 
    println(listOfRideDate.get(i).startdate + " and " + listOfRideDate.get(i).enddate)
}

}

This is my data class and the main method I have added item RideDate of startDate and endDate manually I want delete item dynamically if data contains date1.startdate = "2018-11-05 00:00:00 +0000" date1.enddate = "2018-11-06 23:59:59 +0000"

so that I can ignore it please help me how to delete the item from array list in kotlin

like image 688
MARSH Avatar asked Apr 13 '20 07:04

MARSH


People also ask

How do I remove a mutable list from Kotlin?

Kotlin List – Remove an Element. We can modify the contents of a List only if the list is mutable. To remove a specific element from a Mutable List in Kotlin, call remove() function on this list object and pass the element, that we would like to remove, as argument.


3 Answers

You can use removeAll to remove element from original list if it matches the predicate.

listOfRideDate.removeAll {
    it.startdate == "2018-11-05 00:00:00 +0000" && it.enddate == "2018-11-06 23:59:59 +0000"
}

Or you can filter by creating a new list with filtered items as suggested by Johann Kexel

val filtertedList = listOfRideDate.filter {
    it.startdate == "2018-11-05 00:00:00 +0000" && it.enddate == "2018-11-06 23:59:59 +0000"
}
like image 147
Animesh Sahu Avatar answered Oct 22 '22 01:10

Animesh Sahu


You can use filter in your case

val filtertedList = listOfRideDate.filter { element -> someLogik(element)}

Refer also to the following page

Filtering Kotlinlang

like image 23
Johann Kexel Avatar answered Oct 22 '22 00:10

Johann Kexel


Like this:

loop@ for (i in 0 until listOfRideDate.size) {
        // we have to remove 2018-11-05 00:00:00 +0000 and 2018-11-06 23:59:59 +0000  from list
        if (listOfRideDate.get(i).startdate == "2018-11-05 00:00:00 +0000" &&  listOfRideDate.get(i).enddate == "2018-11-06 23:59:59 +0000"){
            listOfRideDate.removeAt(i)
            break@loop
        }
    }

or

listOfRideDate.removeIf {
     it.startdate == "2018-11-05 00:00:00 +0000" &&  it.enddate == "2018-11-06 23:59:59 +0000"
}
like image 1
Kasım Özdemir Avatar answered Oct 22 '22 00:10

Kasım Özdemir