将List中某个字段相同的数据相加(统计出现了几次)

    xiaoxiao2021-12-04  51

    //取出初始数据 List<LdpaMtxfjlb> LdpaMtxfjlbList = ldpaMtxfjlService.selectAllInfo(); Map<String,Integer> map = new HashMap<>(); //将日期相同的数据进行相加 List<String> same = new ArrayList<>();//接收重复数据的ID boolean flag = false;  //重复标志 String name = "";  //map键值 int sum = 1;          //重复出现次数 for(LdpaMtxfjlb l:LdpaMtxfjlbList){    for(String s:same){  //检测是否出现重复数据 if(l.getId().equals(s)){ flag = true;  //发现重复ID则跳过此次循环 break; } } if(flag){ flag = false; continue; } name = CommUtil.getSimpleDateFormat(l.getDate()); for(LdpaMtxfjlb lm:LdpaMtxfjlbList){ if(l.getId().equals(lm.getId())){//不与自己比较 continue; }else if(l.getDate().equals(lm.getDate())){//检测相同日期的选项 sum ++; same.add(lm.getId()); } } map.put(name, sum); //登记到map sum = 1; //重置sum } for (Map.Entry<String,Integer> entry : map.entrySet()) { //遍历输出      System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());   }   
    转载请注明原文地址: https://ju.6miu.com/read-680348.html

    最新回复(0)