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