DataUtil.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.jjt.biz.util;
  2. import com.jjt.biz.vo.ScoreVO;
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5. /**
  6. * DataUtil$
  7. *
  8. * @author wukai
  9. * @date 2024/10/16 01:52
  10. */
  11. public class DataUtil {
  12. /**
  13. * 补数据
  14. *
  15. * @param standTime
  16. * @param result
  17. */
  18. public static void standTimeVO(List<String> standTime, List<ScoreVO> result) {
  19. for (ScoreVO vo : result) {
  20. // 创建一个新的 times 和 scores 列表,以确保它们与标准时间序列对齐
  21. List<String> alignedTimes = new ArrayList<>();
  22. List<Float> alignedScores = new ArrayList<>();
  23. // 使用一个Map来存储原始数据,以便快速查找
  24. Map<String, Float> timeScoreMap = new HashMap<>();
  25. List<String> times = vo.getXData();
  26. List<Float> scores = vo.getScores();
  27. for (int i = 0; i < times.size(); i++) {
  28. timeScoreMap.put(times.get(i), scores.get(i));
  29. }
  30. // 遍历标准时间序列,并根据需要添加空值或实际值
  31. for (String time : standTime) {
  32. alignedTimes.add(time);
  33. // 添加时间
  34. Float score = timeScoreMap.getOrDefault(time, 100F);
  35. // 如果没有对应的分数,则添加0.0f
  36. alignedScores.add(score);
  37. }
  38. // 更新原有的 times 和 scores 列表
  39. times.clear();
  40. times.addAll(alignedTimes);
  41. scores.clear();
  42. scores.addAll(alignedScores);
  43. }
  44. }
  45. /**
  46. * 补数据
  47. *
  48. * @param standTime
  49. * @param result
  50. */
  51. public static void standTimeMap(List<String> standTime, List<Map<String, Object>> result) {
  52. for (Map<String, Object> map : result) {
  53. // 创建一个新的 times 和 scores 列表,以确保它们与标准时间序列对齐
  54. List<String> alignedTimes = new ArrayList<>();
  55. List<Float> alignedScores = new ArrayList<>();
  56. // 使用一个Map来存储原始数据,以便快速查找
  57. Map<String, Float> timeScoreMap = new HashMap<>();
  58. List<String> times = (List<String>) map.get("time");
  59. List<Float> scores = (List<Float>) map.get("score");
  60. for (int i = 0; i < times.size(); i++) {
  61. timeScoreMap.put(times.get(i), scores.get(i));
  62. }
  63. // 遍历标准时间序列,并根据需要添加空值或实际值
  64. for (String time : standTime) {
  65. alignedTimes.add(time);
  66. // 添加时间
  67. Float score = timeScoreMap.getOrDefault(time, 100F);
  68. // 如果没有对应的分数,则添加0.0f
  69. alignedScores.add(score);
  70. }
  71. // 更新原有的 times 和 scores 列表
  72. times.clear();
  73. times.addAll(alignedTimes);
  74. scores.clear();
  75. scores.addAll(alignedScores);
  76. }
  77. }
  78. /**
  79. * 补数据
  80. *
  81. * @param standTime
  82. * @param result
  83. */
  84. public static void standTimeNet(List<Date> standTime, List<Map<String, Object>> result) {
  85. for (Map<String, Object> map : result) {
  86. // 创建一个新的 times 和 scores 列表,以确保它们与标准时间序列对齐
  87. // 使用一个Map来存储原始数据,以便快速查找
  88. Map<Date, Float> timeScoreMap = new HashMap<>();
  89. List<Map<String, Object>> trendList = (List<Map<String, Object>>) map.get("data");
  90. List<Map<String, Object>> newTrend = new ArrayList<>();
  91. Map<Date, Float> map1 = trendList.stream().collect(Collectors.toMap(mp -> (Date) mp.get("time"), mp -> (Float) mp.get("value")));
  92. for (Date d : map1.keySet()) {
  93. timeScoreMap.put(d, map1.get(d));
  94. }
  95. // 遍历标准时间序列,并根据需要添加空值或实际值
  96. for (Date time : standTime) {
  97. Map<String, Object> map2 = new HashMap<>(16);
  98. map2.put("time", time);
  99. map2.put("value", timeScoreMap.getOrDefault(time, 0F));
  100. newTrend.add(map2);
  101. }
  102. map.remove("data");
  103. map.put("data", newTrend);
  104. }
  105. }
  106. }