前言:
skline1有年份和新申請單位數,skline2有年份和有效期內單位數,我想要把1和2的年份放在一起從小到大放,沒有重復的,新申請單位數和有效期內單位數和年份的排列順序一致
實現:
// 獲取原始數據
List<Map<String, Object>> skLine1 = bmzgConfidentQualifyManageService.getSkLine1();
List<Map<String, Object>> skLine2 = bmzgConfidentQualifyManageService.getSkLine2();// 轉換為年份->數據的Map便于查詢
Map<String, String> yearToCount1 = skLine1.stream().collect(Collectors.toMap(map -> map.get("年份").toString(),map -> map.get("新申請單位數").toString()));Map<String, String> yearToCount2 = skLine2.stream().collect(Collectors.toMap(map -> map.get("年份").toString(),map -> map.get("有效期內單位數").toString()));// 合并年份并排序
Set<String> mergedYears = Stream.concat(skLine1.stream().map(map -> map.get("年份").toString()),skLine2.stream().map(map -> map.get("年份").toString())).collect(Collectors.toCollection(TreeSet::new)); // 自動排序去重// 構建最終結果
List<String> sortedYears = new ArrayList<>(mergedYears);
List<String> newApplications = new ArrayList<>();
List<String> validUnits = new ArrayList<>();for (String year : sortedYears) {newApplications.add(yearToCount1.getOrDefault(year, "0")); // 沒有數據默認為0validUnits.add(yearToCount2.getOrDefault(year, "0"));
}// 最終合并結果
Map<String, Object> result = new HashMap<>();
result.put("xData", sortedYears); // 排序后的年份列表
result.put("yData1", newApplications); // 對應年份的新申請單位數
result.put("yData2", validUnits); // 對應年份的有效期內單位數