概述
@Test
void testBytetoString() {
def errorCodeList = [
[code: "1", language: "2", content: "3"],
[code: "1", language: "2", content: "4"],
[code: "1", language: "3", content: "5"],
[code: "1", language: "3", content: "6"],
[code: "2", language: "1", content: "3"],
[code: "2", language: "2", content: "3"],
[code: "1", language: "2", content: "4"]
]
- 多次
groupBy
def b = errorCodeList.groupBy {
it.code
}.collectEntries {k, v ->
[(k): v.groupBy {
it.language
}.collectEntries {k2, v2 ->
[(k2): v2.collect {
it.content
}]
}]
}
println b
- 自己实现 groupByKey
def a = groupByKey(errorCodeList, "code").collectEntries { k, v ->
def tmpv = groupByKey(v, "language").collectEntries { k2, v2 ->
[(k2): v2.collect {
it["content"]
}]
}
[(k): tmpv]
}
println a
static HashMap<String, List> groupByKey(List<Map> list, String field) {
HashMap store = new HashMap<String, List>()
for(Map map : list) {
String key = map[field]
// map.remove(field)
if (store.containsKey(key)) {
store.replace(key, store.get(key) + map)
} else {
store.put(key, [map])
}
}
store
}
最后
以上就是强健天空为你收集整理的实现 groovy(java) 的groupBy探索和 groupBy 2次的全部内容,希望文章能够帮你解决实现 groovy(java) 的groupBy探索和 groupBy 2次所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复