今天是星期五
2020-05-28 14:13
采纳率: 53.3%
浏览 2.7k
已采纳

Collectors.groupingBy 如何对 List<Map<String,Object>>集合分组

//网上对groupingBy的使用都是针对类对象的分组例子,如List<Person>、List<Student>这种。
//可是我这里有如下数据,应该怎么弄呢?

List<Map<String,Object>> list = new ArrayList<>();
Map<String,Object> map = new HashMap<>();
map.put("dicId","aaa");
map.put("dicName","钢材");
map.put("dicDes","钢材1号");
list.add(map);

map = new HashMap<>();
map.put("dicId","bbb");
map.put("dicName","钢筋");
map.put("dicDes","钢筋1号");
list.add(map);
map = new HashMap<>();
map.put("dicId","111");
map.put("dicName","钢筋");
map.put("dicDes","钢筋2号");
list.add(map);

map = new HashMap<>();
map.put("dicId","ccc");
map.put("dicName","河沙");
map.put("dicDes","河沙1号");
list.add(map);

//比如以dicId分组成为Map<String, List<Map<String,Object>>>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 专业的小白 2020-05-28 17:57
    已采纳
    public void aaa(){
            List<Map<String,Object>> list = new ArrayList<>();
            Map<String,Object> map = new HashMap<>();
            map.put("dicId","aaa");
            map.put("dicName","钢材");
            map.put("dicDes","钢材1号");
            list.add(map);
    
            map = new HashMap<>();
            map.put("dicId","bbb");
            map.put("dicName","钢筋");
            map.put("dicDes","钢筋1号");
            list.add(map);
            map = new HashMap<>();
            map.put("dicId","111");
            map.put("dicName","钢筋");
            map.put("dicDes","钢筋2号");
            list.add(map);
    
            map = new HashMap<>();
            map.put("dicId","ccc");
            map.put("dicName","河沙");
            map.put("dicDes","河沙1号");
            list.add(map);
            Map<String, List<Map<String, Object>>> collect = list.stream().collect(Collectors.groupingBy(this::customKey));
        }
    
        private  String customKey(Map<String,Object> map){
            return map.get("dicId").toString();
        }
    
    点赞 打赏 评论
  • 小码农吗 2020-05-29 09:24
    public static void main(String[] args) {
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            map.put("dicId", "aaa");
            map.put("dicName", "钢材");
            map.put("dicDes", "钢材1号");
            list.add(map);
    
            map = new HashMap<>();
            map.put("dicId", "bbb");
            map.put("dicName", "钢筋");
            map.put("dicDes", "钢筋1号");
            list.add(map);
            map = new HashMap<>();
            map.put("dicId", "111");
            map.put("dicName", "钢筋");
            map.put("dicDes", "钢筋2号");
            list.add(map);
    
            map = new HashMap<>();
            map.put("dicId", "bbb");
            map.put("dicName", "河沙");
            map.put("dicDes", "河沙1号");
            list.add(map);
    
            //比如以dicId分组成为Map<String, List<Map<String,Object>>>
            Map<String, List<Map<String, Object>>> glist = list.stream().collect(Collectors.groupingBy(e -> e.get("dicId").toString()));
    
        }
    
    点赞 1 打赏 评论

相关推荐 更多相似问题