Wesley Acheson
12/18/2019, 12:23 PMGROUP BY day, status
which would give me a list with the same day more than once and the same status more than once which returned a list of
data class DayStatusCount(val day:LocalDate, val status:Status, val count:Long)
If I wanted to convert to a Map<LocalDate, Map<Status, Long>>
or anythings similar to that. I've been creating mutable maps.
val rawData: MutableMap<LocalDate, Map<Status, Long> = HashMap()
data.forEach {
rawData
.getOrPut(it.day) { HashMap() }
.getOrPut(it.status) { HashMap() }
// More as per step
}
Sorry thats not the real code. but anyway convert from a list with duplicates to nested sets.Ubi
12/18/2019, 2:43 PMimport java.time.LocalDate
data class DayStatusCount(val day: LocalDate, val status: String, val count: Long)
val rawData: MutableMap<LocalDate, Map<String, Long>> = HashMap()
val data = listOf(
DayStatusCount(day = LocalDate.of(2001, 1, 1), status = "TODO", count = 1L),
DayStatusCount(day = LocalDate.of(2001, 1, 1), status = "IN_PROGRESS", count = 2L),
DayStatusCount(day = LocalDate.of(2001, 1, 2), status = "TODO", count = 1L),
DayStatusCount(day = LocalDate.of(2001, 1, 3), status = "TODO", count = 1L),
DayStatusCount(day = LocalDate.of(2001, 1, 4), status = "TODO", count = 1L)
)
val groupdData: Map<LocalDate, List<DayStatusCount>> = data.groupBy { it -> it.day }
Ubi
12/18/2019, 2:43 PMdata
? Like the above example?Ruckus
12/18/2019, 2:57 PMval mapped = results.groupBy { it.day }
.mapValues { (_, value) ->
value.groupBy(
{ it.status },
{ it.count }
)
}
Wesley Acheson
12/18/2019, 3:02 PMRuckus
12/18/2019, 3:03 PMfun List<DayStatusCount>.group(): Map<LocalDate, Map<Status, List<Long>>> {
val mapped = mutableMapOf<LocalDate, MutableMap<Status, MutableList<Long>>>()
forEach {
mapped.getOrPut(it.day) { mutableMapOf() }
.getOrPut(it.status) { mutableListOf() } += it.count
}
return mapped
}
(This one will probably perform and scale better.)Wesley Acheson
12/18/2019, 3:04 PMmutableMapOf()
rather than HashMap()
Also I like the array syntax you use. which I'm still getting my head around. Not array exactly more like associative array.Ruckus
12/18/2019, 3:06 PMWesley Acheson
12/18/2019, 3:08 PMWesley Acheson
12/18/2019, 3:08 PMRuckus
12/18/2019, 3:08 PMmutableMapOf
unless I specifically need a HashMap
. (Which isn't all that common, usually I just need a map.)Wesley Acheson
12/18/2019, 3:09 PMRuckus
12/18/2019, 3:09 PMMap
instead of MutableMap
to clean up the API for the user.Wesley Acheson
12/18/2019, 3:11 PMRuckus
12/18/2019, 3:13 PMOf
suffix goes, if I'm not passing anything in, I usually have to add the generic types, so it still reads pretty well to me (in my head this reads as "mutable map of local date to (mutable map of status to long)")Ubi
12/18/2019, 4:31 PMval mapped: Map<LocalDate, Map<String, Long>> = data.groupBy { it.day }
.mapValues { (_, value) -> value.map { it.status to it.count } }
.mapValues { (_, values) -> values.toMap() }
Wesley Acheson
12/18/2019, 4:32 PMUbi
12/18/2019, 4:32 PM