如何在 Webflux 中将 GroupBy 结果列表制作成新地图

How to make GroupBy result list to new Map in Webflux

如何在Webflux中将GroupBy结果列表制作成新的Map

这是我的输入列表和预期结果。那我应该怎么做才能得到结果呢

// expect
{
    "timestamp": "2019-06-13T00:00:00.000",
    "result": {
        "first": 1,
        "second": 2,
        "third": 3
    }
}

// input list
[
    {
        "timestamp": "2019-06-13T00:00:00.000",
        "first": 1
    },
    {
        "timestamp": "2019-06-13T00:00:00.000",
        "second": 2
    },
    {
        "timestamp": "2019-06-13T00:00:00.000",
        "third": 3
    }
]

val Flux.fromIterable(list)
   .groupBy{it.timestamp}
   .concatMap { groupItem ->

     // here!! I want to make `group by result list to new Map``
     Result(timestamp = groupItem.key()!!, Item(first = ?, second = ?, third =?))
   }

我想通了。

Flux.merge(first, second, thrid)
    .groupBy { it.timestamp }
    .concatMap {
        it.map { item ->
            val container = mutableMapOf<String, Any>()
            if (item is firstEntity) {
                container["first"] = item.result.count
                container["timestamp"] = it.key()!!
            }
            if (item is secondEntity) container["second"] = item.result.count
            if (item is thridEntity) container["thrid"] = item.result.count
            container
        }.reduce { acc, current ->
            acc.putAll(current)
            acc
        }
    }
    .map {
        val first = (it["first"] ?: 0) as Int
        val second = (it["second"] ?: 0) as Int
        val thrid = (it["thrid"] ?: 0) as Int
        val timestamp = (it["timestamp"] ?: "") as String

        // somthing!!
    }