算法其实是有用的

工作思考总结:

**算法初体验。**算法就是利用数据结构解决问题,再利用复杂度进行算法评价。
这下面其实就是两种解决方式,也可以叫两种算法。这里就让我了解到了学习算法是有用武之地的。

补全月份的方式:

1、拿到起始的年月使用递增的方式寻找少了的月份,添加到新的数组。
解题思路:抽丝剥茧,就拿出其中的关键信息来思考。找本质!!这里就是月份

@Override
public List<ParkingIndexCharVo> getHistoryTrendChart(BoardReq req) {
    // 获得当年的车位成交量
    List<ParkingIndexCharVo> trendCharts = getTrendChart(req);
    String yearFirstDay = getYearFirstDay();
    List<ParkingIndexCharVo> historyTrendCharts = parkingBoardMapper.historyVolumeTrendChar(req, yearFirstDay);
    ArrayList<ParkingIndexCharVo> parkingIndexCharVos = new ArrayList<>();
    ArrayList<ParkingIndexCharVo> missMonthList = new ArrayList<>();
    if(!CollectionUtil.isEmpty(historyTrendCharts)){
        String monthKey = historyTrendCharts.get(0).getMonthKey();
        for (ParkingIndexCharVo historyTrendVo: historyTrendCharts) {
            if (!monthKey.equals(historyTrendVo.getMonthKey())) {
                ParkingIndexCharVo vo = new ParkingIndexCharVo();
                vo.setMonthKey(monthKey);
                missMonthList.add(vo);
            }
            monthKey = String.valueOf(LocalDate.parse(monthKey).minusMonths(-1));
        }
        while (LocalDate.parse(monthKey).isBefore(LocalDate.parse(yearFirstDay))){
            ParkingIndexCharVo vo = new ParkingIndexCharVo();
            vo.setMonthKey(monthKey);
            missMonthList.add(vo);
            monthKey = String.valueOf(LocalDate.parse(monthKey).minusMonths(-1));
        }
    }
    parkingIndexCharVos.addAll(historyTrendCharts);
    parkingIndexCharVos.addAll(missMonthList);
    parkingIndexCharVos.addAll(trendCharts);
    List<ParkingIndexCharVo> result = parkingIndexCharVos.stream().sorted(Comparator.comparing(ParkingIndexCharVo::getMonthKey)).collect(Collectors.toList());
    return result;
}

2、拿一个全量月份的数组,进行对比添加

public List<ParkingIndexCharVo> getHistoryTrendChart(BoardReq req) {
    // 获得当年的车位成交量
    List<ParkingIndexCharVo> trendCharts = getTrendChart(req);
    String yearFirstDay = getYearFirstDay();
    List<ParkingIndexCharVo> historyTrendCharts = parkingBoardMapper.historyVolumeTrendChar(req, yearFirstDay);
    Map<String, ParkingIndexCharVo> historyCharMap = historyTrendCharts.stream().collect(Collectors.toMap(ParkingIndexCharVo::getMonthKey, t -> t));
    ArrayList<ParkingIndexCharVo> parkingIndexCharVos = new ArrayList<>();
    if (historyTrendCharts.size() > 0) {
        String startDate = historyTrendCharts.get(0).getMonthKey();
        List<String> firstDaysOfMonth = getFirstDaysOfMonth(LocalDate.parse(startDate), LocalDate.parse(yearFirstDay));
        for (String localDate : firstDaysOfMonth) {
            ParkingIndexCharVo vo = new ParkingIndexCharVo();
            vo.setMonthKey(String.valueOf(localDate));
            vo.setMonthActual(null == historyCharMap.get(localDate) ? BigDecimal.ZERO
                : (null == historyCharMap.get(localDate).getMonthActual() ? BigDecimal.ZERO : historyCharMap.get(localDate).getMonthActual()));
            vo.setAveragePrice(null == historyCharMap.get(localDate) ? null : historyCharMap.get(localDate).getAveragePrice());
            parkingIndexCharVos.add(vo);
        }
    }
    parkingIndexCharVos.addAll(trendCharts);
    return parkingIndexCharVos;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值