简体   繁体   English

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

[英]How to make GroupBy result list to new Map in Webflux

How to make GroupBy result list to new Map in Webflux如何在 Webflux 中将 GroupBy 结果列表制作为新地图

There is my input list and expect result.有我的输入列表和期望的结果。 then what should I do to make the result.那我该怎么做才能得到结果。

// 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 =?))
   }

I figured it out.我想到了。

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!!
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM