多个List 合并变成一个List+一个List 根据某个字段相等的另一个字段相加,并排序变成新的List

List curves = new ArrayList<>();
        for (int i = 0; i < columnNames.size(); i++){
            if (columnNames.get(i).equals(PlantConstant.TENDOWNFX) || columnNames.get(i).equals(PlantConstant.TENDOWNQP)) {
                //10千伏以下 数据 进行缓慢处理
                curveTimeAndValueDomains = gently(belongDay, columnNames.get(i));
            }else{
                //一个一个场站加
                curveTimeAndValueDomains = curveManagerCommonMapper.selectSinglePlantPowerDay2(belongDay, columnNames.get(i), belongDay.substring(0, 4));
            }
            if(!Objects.isNull(curveTimeAndValueDomains)){
                for (CurveTimeAndValueDomain curve:curveTimeAndValueDomains){
                    curves.add(curve);
                }
            }
        }

第一步:将多个List 相加

for (CurveTimeAndValueDomain curve:curveTimeAndValueDomains){
                    curves.add(curve);
                }

 第二步:将加起来的List 中:time 相同的value值相加

//将曲线中 time 相同的 value 相加 并 去重
        List  newCurve = merge(curves);

public static List merge(List curves){
        Map map = new HashMap<>();
        curves.stream().forEach(curve ->{
            CurveTimeAndValueDomain last = map.get(curve.getTime());
            if (null != last){
                if(!Objects.isNull(last.getValue())){
                    curve.setValue(curve.getValue().add(last.getValue()));
                }
            }
            map.put(curve.getTime(),curve);
        });
        return map.values().stream().collect(Collectors.toList());
    }

第三步:将新的到的newCurve  List 排序

//按照time 进行排序
        newCurve.sort((curve1,curve2)->{
            Integer time1 = Integer.parseInt(curve1.getTime().replace(":",""));
            Integer time2 = Integer.parseInt(curve2.getTime().replace(":",""));
            return time1.compareTo(time2);
        });

你可能感兴趣的:(list,java,算法)