我是靠谱客的博主 羞涩机器猫,这篇文章主要介绍java和groovy对List中的Map按照某一个key的值相同的进行分组,现在分享给大家,希望可以做个参考。

groovy中

import java.util.stream.Collectors

List params = []
Map temp = [:]
temp.put("id",1)
temp.put("name","qq")
temp.put("height","180")
params.add(temp)
temp = new HashMap()
temp.put("id",1)
temp.put("name","www")
temp.put("height","111")
params.add(temp)
temp = new HashMap()
temp.put("id",2)
temp.put("name","eee")
temp.put("height","222")
params.add(temp)

Map<Integer, List<Map>> groupBy = params.stream().collect(Collectors.groupingBy({it.id}))
System.out.println(groupBy)

排序后的结果是一个Map,它的key是分组的组号,value是List,List中是分组后的Map
在这里插入图片描述

java中

List<Map<String,Object>> params = new ArrayList<>();
Map<String,Object> temp = new HashMap<>();
temp.put("id",1);
temp.put("name","qq");
temp.put("height","180");
params.add(temp);
temp = new HashMap<>();
temp.put("id",1);
temp.put("name","www");
temp.put("height","111");
params.add(temp);
temp = new HashMap<>();
temp.put("id",2);
temp.put("name","eee");
temp.put("height","222");
params.add(temp);

Map groupBy = params.stream().collect(Collectors.groupingBy(it->it.get("id")));
System.out.println(groupBy);

在这里插入图片描述

最后

以上就是羞涩机器猫最近收集整理的关于java和groovy对List中的Map按照某一个key的值相同的进行分组的全部内容,更多相关java和groovy对List中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(119)

评论列表共有 0 条评论

立即
投稿
返回
顶部