
本文介绍如何使用 java 的现代时间 api(java.time)解析带毫秒精度的时间字符串,筛选高分记录(score > 20),按时间升序/降序组织,并高效识别同一 id 在 2 分钟内是否集中出现 ≥5 条记录,最终输出该时间窗口的最早时间戳及对应 arcade id。
在 Arcade 排行榜或实时评分系统中,常需从原始日志中快速识别“爆发式高分行为”——例如同一设备(ID)在极短时间内连续提交多个高分。本教程提供一套健壮、可读性强且符合 Java 最佳实践的解决方案。
✅ 核心设计原则
- 弃用 Date / SimpleDateFormat:全部采用 java.time 新 API(JDK 8+),避免线程安全与时区歧义问题;
- 使用 record 封装数据:轻量、不可变、自动生成 equals/toString,天然适配函数式处理;
- 时间解析精准到毫秒:通过 DateTimeFormatter.ofPattern("uuuuMMdd HH:mm:ss.SSS") 严格匹配输入格式;
- 滑动窗口检测高效可靠:不依赖暴力嵌套循环,而是基于排序后列表 + 双指针(two-pointer)思想实现 O(n) 时间复杂度的区间扫描。
? 示例代码实现(完整可运行)
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
public class ArcadeScoreAnalyzer {
record Score(int id, LocalDateTime when, int score) {}
public static void main(String[] args) {
String input = """
501,20220104 13:12:07.005,25
501,20220104 13:12:07.002,25
500,20220106 09:04:10.013,10
501,20220104 13:12:07.001,25
501,20220104 13:12:07.003,25
501,20220104 13:12:07.004,25
501,20220104 15:20:50.011,25
""";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMdd HH:mm:ss.SSS");
List allScores = new ArrayList<>();
for (String line : input.strip().lines().toList()) {
if (line.trim().isEmpty()) continue;
String[] parts = line.split(",", 3);
if (parts.length != 3) throw new IllegalArgumentException("Invalid line: " + line);
int id = Integer.parseInt(parts[0].trim());
LocalDateTime when = LocalDateTime.parse(parts[1].trim(), formatter);
int score = Integer.parseInt(parts[2].trim());
allScores.add(new Score(id, when, score));
}
// Step 1: Filter scores > 20
List highScores = allScores.stream()
.filter(s -> s.score() > 20)
.collect(Collectors.toList());
// Step 2: Group by ID, then process each group
Map> byId = highScores.stream()
.collect(Collectors.groupingBy(Score::id));
// Step 3: For each ID, check 2-minute window (120 seconds) containing ≥5 entries
Duration window = Duration.ofMinutes(2);
List results = new ArrayList<>();
for (Map.Entry> entry : byId.entrySet()) {
List list = entry.getValue().stream()
.sorted(Comparator.comparing(Score::when)) // ascending: earliest first
.collect(Collectors.toList());
if (list.size() < 5) continue;
// Two-pointer sliding window to find earliest 5-in-window start time
for (int i = 0; i <= list.size() - 5; i++) {
LocalDateTime start = list.get(i).when();
LocalDateTime end = start.plus(window);
// Count how many entries fall in [start, end)
long count = list.stream()
.filter(s -> !s.when().isBefore(start) && s.when().isBefore(end))
.count();
if (count >= 5) {
results.add(new ScoreResult(entry.getKey(), start, count));
break; // found earliest valid window for this ID → exit inner loop
}
}
}
// Output result(s)
if (!results.isEmpty()) {
ScoreResult first = results.stream()
.min(Comparator.comparing(ScoreResult::earliestTime))
.orElseThrow();
System.out.printf("✅ Found qualifying burst: ID=%d, earliest time=%s, count=%d%n",
first.id(), formatOutputTime(first.earliestTime()), first.count());
} else {
System.out.println("❌ No ID has ≥5 scores > 20 within any 2-minute window.");
}
}
// Helper to reformat LocalDateTime for output (e.g., "20220104 13:12:07.001")
private static String formatOutputTime(LocalDateTime dt) {
return dt.format(DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSS"));
}
// Lightweight result holder
private static record ScoreResult(int id, LocalDateTime earliestTime, long count) {}
} ⚠️ 关键注意事项
- 时间比较必须用 isBefore() / isAfter():LocalDateTime 不支持 + 运算符,切勿写 dt + 2 minutes;应使用 dt.plus(Duration);
- Half-Open 区间语义:定义 [start, start + 2min) 更严谨,避免边界重复计数;
- 排序方向决定逻辑清晰度:先按 when 升序排列,再用双指针从左向右扫描,自然获得“最早满足条件的起始时间”;
- 性能优化提示:若数据量极大(如百万级),可将滑动窗口逻辑改写为单次遍历(维护一个队列),避免对每个 i 都做流式计数;
-
异常防御:务必校验 CSV 字段数、数字解析异常、空行等,生产环境建议封装为 Optional
或自定义 Result 类型。
✅ 总结
本方案以清晰的数据建模(record)、标准的时间解析(DateTimeFormatter)和高效的滑动窗口算法,完整实现了题目全部需求:筛选、分组、时间窗判定与结果提取。它不仅解决当前问题,更构建了可扩展的评分分析骨架——后续添加“去重 IP”、“跨天窗口”或“动态阈值”等功能均可在此基础上平滑演进。









