工作思考总结:
**算法初体验。**算法就是利用数据结构解决问题,再利用复杂度进行算法评价。
这下面其实就是两种解决方式,也可以叫两种算法。这里就让我了解到了学习算法是有用武之地的。
补全月份的方式:
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;
}