Reputation: 173
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 code I have right now. I can't use anything else, because when I'm using filter to my arraylist parkingList, it won't work. And I don't know why. This 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 it won't work. P.S. I need only API19, so, this is the reason, why I must use only filter (Or no?)
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)
Upvotes: 4
Views: 275
Reputation: 1
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")
Upvotes: 0
Reputation: 14173
removeIf
will envetually remove elements from the list on 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 returned value.
val originalList = listOf(1,2,3,4)
val filteredList = originalList.filter { it % 2 == 0 }
// originalList.size is 4
// filteredList.size is 2
Also, when you check for equality with parking.city === pr.city
, that will return true only if parking.city
and pr.city
are the same object, it will return false even if the 2 objects have equal values, that is, if equals(Any)
returns true
update
In your code you have 4 boolean expressions but only the last one is returned, you need to chain them with an operator, I added &&
as an example but you need adjust that based on your business logic
filter { parking ->
parking.city === pr.city &&
parking.price.toDouble() >= pr.priceFrom &&
parking.price.toDouble() <= pr.priceTo &&
!parking.daysBusy.contains(daysSet.split("|").toString())
}
For instance, if you try to filter with
.filter {
false
true
}
All elements will be kept be cause the first false
is going to be completely ignored, if you instead do
.filter {
false &&
true
}
The filtered list will have 0 elements because the filtering expression is now false && true
Upvotes: 5