将List结果集转成Map<key,Object>或者Map<key,List>

业务背景:有时候我们需要循环List的某个属性,和固定的这个属性List进行比较,如果使用双层for循环 势必形象性能,所以我们可以将List转换成Map,key值就是那个属性,value就是List的里的对象或者,value就是list里的某些List,

譬如 1-24时的每个小时的数据或者数据集合,拿hour作为key将这个List转成Map,然后循环这个1-24个小时的固定list,然后从map里取hour对应key的value,如果非空的话 则表示原list存在,否则不存在,该补0补0该跳过跳过。上代码:

value是List里的元素的情况一:

List result=venueManagementDayMapper.venueCheckTrendMonth(startOfMonth,today,deptCode);
        Map trendMap = result.stream().collect(Collectors.toMap(VenueCheckTrendODTO::getIndex, Function.identity()));
        int dayOfMonth = DateUtil.getDayOfMonth();
        int currentMonthNumber = DateUtil.getCurrentMonthNumber();
        for(Integer day=1;day<=dayOfMonth;day++){
            VenueCheckTrendODTO venueCheckTrendODTO = trendMap.get(day.toString());
            if(venueCheckTrendODTO==null){
                venueCheckTrendODTO=new VenueCheckTrendODTO();
                venueCheckTrendODTO.setCheckNum(0);
                venueCheckTrendODTO.setIndex(day.toString());
                result.add(venueCheckTrendODTO);
            }
        }
//此处是对list进行一个重排序
        result= result.stream().sorted(Comparator.comparing(VenueCheckTrendODTO::getIndex,Comparator.comparingInt(Integer::parseInt))).collect(Collectors.toList());
        result.stream().forEach(trend->{
            trend.setIndex(currentMonthNumber+"-"+trend.getIndex());
        });

value 是list里的一些对象的集合,如果7点可能对应多条数据,这样就需要用的分组了。

List trends=iocCarflowHourPreMapper.selectCarFlowHourTrends(today,deptCode);
        int curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        List actualList=iocCarflowHourMapper.selectCarFlowHourActuals(today,curHour,deptCode);
        Map map=trends.stream().collect(Collectors.toMap(CarTrendHourODTO::getIndex, Function.identity()));
        Map> actualMap=actualList.stream().collect(Collectors.groupingBy(IocCarflowHour::getEnterHour));
        for(Integer hour=7;hour<=23;hour++){
            CarTrendHourODTO carTrendHourODTO=  map.get(hour.toString());
            if(carTrendHourODTO==null){
                //车流预测数据为空需要补0
                carTrendHourODTO=new CarTrendHourODTO();
                //如果没有数据则为空串
                if(curHour>hour){
                    carTrendHourODTO.setTrendAmount("0");
                }else{
                    carTrendHourODTO.setTrendAmount("0");
                }
                trends.add(carTrendHourODTO);
            }
            //车流预测数据非空,判定实际数据是否为空,如果为空则补0
            List iocCarflowHourList = actualMap.get(hour);
            if(iocCarflowHourList==null){
                if(curHour>hour){
                    carTrendHourODTO.setNum11Amount("0");
                    carTrendHourODTO.setNum12Amount("0");
                    carTrendHourODTO.setNum2Amount("0");
                    carTrendHourODTO.setTotalAmount("0");
                }else{
                    carTrendHourODTO.setNum11Amount("");
                    carTrendHourODTO.setNum12Amount("");
                    carTrendHourODTO.setNum2Amount("");
                    carTrendHourODTO.setTotalAmount("");
                }
            }else{
                for(IocCarflowHour iocCarflowHour:iocCarflowHourList){
                    if(hour.toString().equals(iocCarflowHour.getEnterHour().toString())){
                        if(CommonConstant.PARK1_1_POI_CODE.equals(iocCarflowHour.getPoiCode())){
                            carTrendHourODTO.setNum11Amount(iocCarflowHour.getStayNum().toString());
                        }else if(CommonConstant.PARK1_2_POI_CODE.equals(iocCarflowHour.getPoiCode())){
                            carTrendHourODTO.setNum12Amount(iocCarflowHour.getStayNum().toString());
                        }else if(CommonConstant.PARK2_POI_CODE.equals(iocCarflowHour.getPoiCode())){
                            carTrendHourODTO.setNum2Amount(iocCarflowHour.getStayNum().toString());
                        }
                    }
                }
                carTrendHourODTO.setTotalAmount((Ints.tryParse(carTrendHourODTO.getNum11Amount())+Ints.tryParse(carTrendHourODTO.getNum12Amount())+Ints.tryParse(carTrendHourODTO.getNum2Amount()))+"");
            }
            carTrendHourODTO.setIndex((hour<10?"0"+hour:hour.toString())+":00");
        }
        return trends;

上面的其实是两个业务list,其中第一个list是预期曲线的值,横坐标是hour,第二个list是实际曲线的值,横坐标也是hour,但是第二个list,一条hour对应三条数据,因此使用Collectors.groupingBy

actualList.stream().collect(Collectors.groupingBy(IocCarflowHour::getEnterHour));对hour的数据进行分组,其输出值则为Map类型,那边下面在依次对hour映射的value list进行for循环,如图

将List结果集转成Map<key,Object>或者Map<key,List>_第1张图片

 

你可能感兴趣的:(java,算法,数学建模)