stream对map的list数据集分组求和


List<Map<String,Object>> list = new ArrayList<>();
      Map<String,Object> stu1 = new HashMap<String, Object>();
      stu1.put("name", "卫庄");
      stu1.put("score", new BigDecimal(80));
      list.add(stu1);
      Map<String,Object> stu2 = new HashMap<String, Object>();
      stu2.put("name", "盖聂");
      stu2.put("score", new BigDecimal(90));
      list.add(stu2);
      Map<String,Object> stu3 = new HashMap<String, Object>();
      stu3.put("name", "天明");
      stu3.put("score", new BigDecimal(60));
      list.add(stu3);
      Map<String,Object> stu4 = new HashMap<String, Object>();
      stu4.put("name", "卫庄");
      stu4.put("score", new BigDecimal(30));
      list.add(stu4);
      list = list.stream().collect(Collectors.groupingBy(d -> d.get("name"))).values().stream().map(d -> {
            Map<String, Object> sampleData = d.get(0);
            sampleData.put("score", d.stream().map(s -> new BigDecimal(s.get("score").toString())).reduce(BigDecimal.ZERO, BigDecimal::add));
            return sampleData;
        }).collect(Collectors.toList());
      System.out.println(list);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM