oday
03/04/2019, 3:55 PMname
field, then reconstruct this sortedmap agianPavlo Liapota
03/04/2019, 4:15 PModay
03/04/2019, 4:37 PMPavlo Liapota
03/04/2019, 5:14 PMTreeMap
is first implementation of SortedMap
that I found.
Kotlin’s .toSortedMap()
also creates TreeMap
oday
03/04/2019, 5:16 PMPavlo Liapota
03/04/2019, 5:16 PMmapValues
creates a new map and it is not SortedMap
.oday
03/04/2019, 5:19 PMtoSortedMap
inside it, i was doing it twice before, it was wrongtosortedMap
isnt letting me do this because it needs a comparatorPavlo Liapota
03/04/2019, 6:41 PMval carMakesMap = carMakes.values
.flatten()
.filter { carMake -> carMake.name.contains(newText, ignoreCase = true) }
.groupBy { it.name.first() }
Or maybe:
val carMakesMap = carMakes
.mapValues { (_, value) ->
value.filter {carMake -> carMake.name.contains(newText, ignoreCase = true) }
}
.filterValues { it.isNotEmpty() }
kevinmost
03/04/2019, 6:52 PM.functionalOperator { ... }.toCustomCollectionImpl()
with .functionalOperatorTo(CustomCollectionImpl()) { ... }
.mapValues { ... }.toSortedMap()
with .mapValuesTo(TreeMap()) { ... }
oday
03/04/2019, 7:42 PMcarMakes.mapValues { entry ->
entry.value.filter { carMake ->
carMake.name.contains(newText, ignoreCase = true)
}.groupBy { it.name.first() }.toMap(carMakesMap)
}
but i am still unable to then after all of this, filter the keys to only take those who have notEmpty values (the list belonging to that key is empty because none of its items matched that key (entered through searchbar))carMakes.mapValues { entry ->
entry.value.filter { carMake ->
carMake.name.contains(newText, ignoreCase = true)
}.groupBy { it.name.first() }.toMap(carMakesMap)
}.filterKeys { carMakes[it]!!.isNotEmpty() }
kevinmost
03/04/2019, 7:47 PMmapValues
, and then the output collection isn't in a format that you want so you do something like toMap(...)
or toSortedMap()
or anything like that, you could use the version of the transform ending in To
, like mapValuesTo
and specify a collection to output into as the first parameteroday
03/04/2019, 7:47 PMkevinmost
03/04/2019, 7:49 PMChar
keys are like, the first letter in the car make? Like the map is:
[
'a': [],
'b': [],
'c': [CarMake("Chevy")],
'd': [CarMake("Dodge")],
...
]
?oday
03/04/2019, 7:51 PM.filterKeys
on it after i filtered the valueskevinmost
03/04/2019, 7:52 PModay
03/04/2019, 7:53 PM.filterKeys { carMakes[it].isNotEmpty() }
kevinmost
03/04/2019, 8:00 PMcarMakes
.mapValuesTo(TreeMap()) { (_, values: List<String>) -> values.filter { it.contains(filter, ignoreCase = true) }}
.filterTo(TreeMap()) { (_, values) -> values.isNotEmpty() }
is there something about this that wouldn't work?oday
03/04/2019, 8:01 PMkevinmost
03/04/2019, 8:02 PMcarMakes
.values
.flatten()
.filter { it.contains(filter, ignoreCase = true) }
.groupByTo(TreeMap()) { it[0] }
asSequence()
in the middle too. .values.asSequence().flatten()....
oday
03/04/2019, 8:27 PM