ArrayList.filter will not work

So, I have this code for Android:

parkingList.removeIf { parking-> parking.city != pr.city }
parkingList.removeIf { parking-> parking.price.toDouble() <= pr.priceFrom }
parkingList.removeIf { parking-> parking.price.toDouble() >= pr.priceTo }
parkingList.removeIf { parking-> parking.daysBusy.contains(daysSet.split("|").toString()) }

This is the code that I have right now. I cannot use anything else, because when I use the filter in my arraylist parking lot, it will not work. And I do not know why. Here is how I use it:

parkingList.filter { parking-> parking.city === pr.city }
parkingList.filter { parking-> parking.price.toDouble() >= pr.priceFrom }
parkingList.filter { parking-> parking.price.toDouble() <= pr.priceTo }
parkingList.filter { parking-> !parking.daysBusy.contains(daysSet.split("|").toString()) }

But for some reason this will not work. Postscript I only need API19, therefore, this is the reason why I should use only a filter (or not?)

CODE:

val filteredList = parkingList.filter { parking-> parking.city === pr.city
        parking.price.toDouble() >= pr.priceFrom
        parking.price.toDouble() <= pr.priceTo
        !parking.daysBusy.contains(daysSet.split("|").toString())
    } as ArrayList

filteredList.forEach { println(it) }
val adapter = CustomAdapter(filteredList)
+4
source share
2 answers

removeIf It will be flexible to remove items from the list to which you call it, it works in place.

val originalList = arrayListOf(1,2,3,4)
originalList.removeIf { it % 2 == 0 }
// originalList.size is 2

filter, on the other hand, will return a new list, so you need to use the return value.

val originalList = listOf(1,2,3,4)
val filteredList = originalList.filter { it % 2 == 0 }
// originalList.size is 4
// filteredList.size is 2

, parking.city === pr.city, true, parking.city pr.city , false, 2 , equals(Any) true

4 , , , && , -

filter { parking ->
    parking.city === pr.city &&
    parking.price.toDouble() >= pr.priceFrom &&
    parking.price.toDouble() <= pr.priceTo &&
    !parking.daysBusy.contains(daysSet.split("|").toString())
}

,

.filter {
    false
    true
}

, false ,

.filter {
    false &&
    true
}

0 , false && true

+5
You can use this code to filter out the element from array, by using this code:

    var day: List = arrayListOf("Sunday", "Monday", "Tuesday","Wednesday")


// to get the result as list


      var dayList: List = day.filter { s -> s == "Monday" }

// to get a a string

    var selectedDay: String = day.filter { s -> s == "`enter code here`Monday" }.single() 


     println("the value  is $dayList")
println("the value is $selectedDay")

0

Source: https://habr.com/ru/post/1694107/


All Articles