原创

利用Map格式化数据并进行升降序排序

转化之前以设备分组每天24个点的数据:
[
EnergyTotalDTO(meteringName=108D出线柜电表, dataList=[]),
EnergyTotalDTO(meteringName=103D电容柜电表, dataList=[{date=00:00, value=28.0}, {date=01:00, value=26.0}, {date=02:00, value=33.0}, {date=03:00, value=30.0}, {date=04:00, value=29.0}, {date=05:00, value=31.0}, {date=06:00, value=26.0}, {date=07:00, value=35.0}, {date=08:00, value=30.0}, {date=09:00, value=30.0}, {date=10:00, value=28.0}, {date=11:00, value=29.0}, {date=12:00, value=35.0}, {date=13:00, value=23.0}, {date=14:00, value=29.0}, {date=15:00, value=31.0}, {date=16:00, value=26.0}, {date=17:00, value=30.0}, {date=18:00, value=30.0}, {date=19:00, value=35.0}, {date=20:00, value=33.0}, {date=21:00, value=28.0}, {date=22:00, value=35.0}, {date=23:00, value=32.0}]),
EnergyTotalDTO(meteringName=107D出线柜电表, dataList=[{date=00:00, value=33.0}, {date=01:00, value=26.0}, {date=02:00, value=28.0}, {date=03:00, value=32.0}, {date=04:00, value=33.0}, {date=05:00, value=31.0}, {date=06:00, value=33.0}, {date=07:00, value=32.0}, {date=08:00, value=26.0}, {date=09:00, value=36.0}, {date=10:00, value=37.0}, {date=11:00, value=30.0}, {date=12:00, value=31.0}, {date=13:00, value=32.0}, {date=14:00, value=29.0}, {date=15:00, value=27.0}, {date=16:00, value=33.0}, {date=17:00, value=30.0}, {date=18:00, value=27.0}, {date=19:00, value=32.0}, {date=20:00, value=29.0}, {date=21:00, value=28.0}, {date=22:00, value=22.0}, {date=23:00, value=33.0}]),
EnergyTotalDTO(meteringName=106D出线柜电表, dataList=[{date=00:00, value=31.0}, {date=01:00, value=28.0}, {date=02:00, value=28.0}, {date=03:00, value=31.0}, {date=04:00, value=27.0}, {date=05:00, value=31.0}, {date=06:00, value=26.0}, {date=07:00, value=28.0}, {date=08:00, value=32.0}, {date=09:00, value=35.0}, {date=10:00, value=33.0}, {date=11:00, value=26.0}, {date=12:00, value=32.0}, {date=13:00, value=30.0}, {date=14:00, value=29.0}, {date=15:00, value=26.0}, {date=16:00, value=29.0}, {date=17:00, value=27.0}, {date=18:00, value=21.0}, {date=19:00, value=27.0}, {date=20:00, value=32.0}, {date=21:00, value=32.0}, {date=22:00, value=29.0}, {date=23:00, value=30.0}])
]

转换排序以后按时间分组每个时间点各个设备的数据:
[
TotalPowerDTO(time=00:00, values=[{meteringName=103D电容柜电表, value=28.0}, {meteringName=107D出线柜电表, value=33.0}, {meteringName=106D出线柜电表, value=31.0}]),
TotalPowerDTO(time=01:00, values=[{meteringName=103D电容柜电表, value=26.0}, {meteringName=107D出线柜电表, value=26.0}, {meteringName=106D出线柜电表, value=28.0}]),
TotalPowerDTO(time=02:00, values=[{meteringName=103D电容柜电表, value=33.0}, {meteringName=107D出线柜电表, value=28.0}, {meteringName=106D出线柜电表, value=28.0}]),
TotalPowerDTO(time=03:00, values=[{meteringName=103D电容柜电表, value=30.0}, {meteringName=107D出线柜电表, value=32.0}, {meteringName=106D出线柜电表, value=31.0}]),
TotalPowerDTO(time=04:00, values=[{meteringName=103D电容柜电表, value=29.0}, {meteringName=107D出线柜电表, value=33.0}, {meteringName=106D出线柜电表, value=27.0}]),
TotalPowerDTO(time=05:00, values=[{meteringName=103D电容柜电表, value=31.0}, {meteringName=107D出线柜电表, value=31.0}, {meteringName=106D出线柜电表, value=31.0}]),
TotalPowerDTO(time=06:00, values=[{meteringName=103D电容柜电表, value=26.0}, {meteringName=107D出线柜电表, value=33.0}, {meteringName=106D出线柜电表, value=26.0}]),
TotalPowerDTO(time=07:00, values=[{meteringName=103D电容柜电表, value=35.0}, {meteringName=107D出线柜电表, value=32.0}, {meteringName=106D出线柜电表, value=28.0}]),
TotalPowerDTO(time=08:00, values=[{meteringName=103D电容柜电表, value=30.0}, {meteringName=107D出线柜电表, value=26.0}, {meteringName=106D出线柜电表, value=32.0}]),
TotalPowerDTO(time=09:00, values=[{meteringName=103D电容柜电表, value=30.0}, {meteringName=107D出线柜电表, value=36.0}, {meteringName=106D出线柜电表, value=35.0}]),
TotalPowerDTO(time=10:00, values=[{meteringName=103D电容柜电表, value=28.0}, {meteringName=107D出线柜电表, value=37.0}, {meteringName=106D出线柜电表, value=33.0}]),
TotalPowerDTO(time=11:00, values=[{meteringName=103D电容柜电表, value=29.0}, {meteringName=107D出线柜电表, value=30.0}, {meteringName=106D出线柜电表, value=26.0}]),
TotalPowerDTO(time=12:00, values=[{meteringName=103D电容柜电表, value=35.0}, {meteringName=107D出线柜电表, value=31.0}, {meteringName=106D出线柜电表, value=32.0}]),
TotalPowerDTO(time=13:00, values=[{meteringName=103D电容柜电表, value=23.0}, {meteringName=107D出线柜电表, value=32.0}, {meteringName=106D出线柜电表, value=30.0}]),
TotalPowerDTO(time=14:00, values=[{meteringName=103D电容柜电表, value=29.0}, {meteringName=107D出线柜电表, value=29.0}, {meteringName=106D出线柜电表, value=29.0}]),
TotalPowerDTO(time=15:00, values=[{meteringName=103D电容柜电表, value=31.0}, {meteringName=107D出线柜电表, value=27.0}, {meteringName=106D出线柜电表, value=26.0}]),
TotalPowerDTO(time=16:00, values=[{meteringName=103D电容柜电表, value=26.0}, {meteringName=107D出线柜电表, value=33.0}, {meteringName=106D出线柜电表, value=29.0}]),
TotalPowerDTO(time=17:00, values=[{meteringName=103D电容柜电表, value=30.0}, {meteringName=107D出线柜电表, value=30.0}, {meteringName=106D出线柜电表, value=27.0}]),
TotalPowerDTO(time=18:00, values=[{meteringName=103D电容柜电表, value=30.0}, {meteringName=107D出线柜电表, value=27.0}, {meteringName=106D出线柜电表, value=21.0}]),
TotalPowerDTO(time=19:00, values=[{meteringName=103D电容柜电表, value=35.0}, {meteringName=107D出线柜电表, value=32.0}, {meteringName=106D出线柜电表, value=27.0}]),
TotalPowerDTO(time=20:00, values=[{meteringName=103D电容柜电表, value=33.0}, {meteringName=107D出线柜电表, value=29.0}, {meteringName=106D出线柜电表, value=32.0}]),
TotalPowerDTO(time=21:00, values=[{meteringName=103D电容柜电表, value=28.0}, {meteringName=107D出线柜电表, value=28.0}, {meteringName=106D出线柜电表, value=32.0}]),
TotalPowerDTO(time=22:00, values=[{meteringName=103D电容柜电表, value=35.0}, {meteringName=107D出线柜电表, value=22.0}, {meteringName=106D出线柜电表, value=29.0}]),
TotalPowerDTO(time=23:00, values=[{meteringName=103D电容柜电表, value=32.0}, {meteringName=107D出线柜电表, value=33.0}, {meteringName=106D出线柜电表, value=30.0}])
]

因为项目中前台展示需要用到时间分组展示,类似于table一样一行数据,就需要将原来的数据格式进行转换,博主这里用到的是map的方式转换,然后理由Java8的新特性进行排序
//封装表对应行数据
Map totalPowerDTOMap = new HashMap();
//此处energyTotalDTOS就是转换之前封装的数据,进行遍历
for (EnergyTotalDTO energyTotalDTO : energyTotalDTOS) {
String meteringName = energyTotalDTO.getMeteringName();
List> dataList = energyTotalDTO.getDataList();

        for (Map<String, Object> data : dataList) {
        //获取到时间
            String dateTime = (String) data.get("date");
            data.put("meteringName", meteringName);
            //将拿到以后的时间从map中剔除了
            data.remove("date");
            TotalPowerDTO totalPowerDTO = totalPowerDTOMap.get(dateTime);
            //如果totalPowerDTO以该时间开始的这个对象是空的 则重新创建新的对象,并添加时间,否则只需要继续添加数据即可
            if (null == totalPowerDTO) {
                totalPowerDTO = new TotalPowerDTO();
                totalPowerDTO.setTime(dateTime);

                totalPowerDTO.getValues().add(data);
            } else {
                totalPowerDTO.getValues().add(data);
            }
            //最后遍历完一次放入map中,因为dateTime是不一样的,所以不会被覆盖
            totalPowerDTOMap.put(dateTime, totalPowerDTO);
        }
    }
    //将map转为list
     List<TotalPowerDTO> listTotalP = new ArrayList<TotalPowerDTO>(totalPowerDTOMap.values());
     //这里是博主进行的一个升降序的判断,0为升序,否则降序。下面这个stream().sorted就是排序方法,相对于自己写算法简单了很多
    if(sortType==0){
        listTotalP = listTotalP.stream().sorted(Comparator.comparing(TotalPowerDTO::getTime)).collect(Collectors.toList());
    }else {
        listTotalP = listTotalP.stream().sorted(Comparator.comparing(TotalPowerDTO::getTime).reversed()).collect(Collectors.toList());
    }



    注:
    TotalPowerDTO这个类的属性为
        //单一时间
        private String time;
        //该时间下对应的各个计量点值
        private List<Map<String,Object>> values = new ArrayList<>();
    EnergyTotalDTO这个类的属性为
        private String meteringName;
        private List<Map<String, Object>> dataList;


        完成以上一系列操作以后就得到了一时间分组的数据,这就是博主日常代码时遇到的问题,记录下来,或许帮得到大家,或许没啥用,但是对于我而已,还是很有用处,因为有时候想不到这些,
        或者自己想复杂了,所以记录下来,以后遇到类似的时候翻翻博客也挺好的。
正文到此结束
本文目录