Java 案例:统计区域防守漏洞出现次数
下面是一个完整的 Java 案例,模拟统计区域防守漏洞出现次数,涵盖统计逻辑、分组统计、TOP 排名等常见需求。

完整代码
import java.util.*;
import java.util.stream.Collectors;
/**
* 区域防守漏洞统计案例
*/
public class DefenseVulnerabilityStats {
/** 防守区域枚举 */
enum Zone {
LEFT_FLANK("左路"), CENTER("中路"), RIGHT_FLANK("右路"),
PENALTY_AREA("禁区"), MIDFIELD("中场");
private final String desc;
Zone(String desc) { this.desc = desc; }
public String getDesc() { return desc; }
}
/** 漏洞记录 */
static class VulnerabilityRecord {
private final String matchId; // 比赛ID
private final Zone zone; // 防守区域
private final String type; // 漏洞类型:站位/协防/盯人/回追
private final int minute; // 发生时间(分钟)
public VulnerabilityRecord(String matchId, Zone zone, String type, int minute) {
this.matchId = matchId;
this.zone = zone;
this.type = type;
this.minute = minute;
}
public String getMatchId() { return matchId; }
public Zone getZone() { return zone; }
public String getType() { return type; }
public int getMinute() { return minute; }
@Override
public String toString() {
return String.format("[%s] %s-%s (第%d分钟)",
matchId, zone.getDesc(), type, minute);
}
}
public static void main(String[] args) {
// 1. 模拟数据
List<VulnerabilityRecord> records = Arrays.asList(
new VulnerabilityRecord("M001", Zone.LEFT_FLANK, "站位", 12),
new VulnerabilityRecord("M001", Zone.LEFT_FLANK, "回追", 35),
new VulnerabilityRecord("M001", Zone.CENTER, "协防", 58),
new VulnerabilityRecord("M001", Zone.PENALTY_AREA, "盯人", 77),
new VulnerabilityRecord("M002", Zone.LEFT_FLANK, "站位", 8),
new VulnerabilityRecord("M002", Zone.RIGHT_FLANK, "盯人", 23),
new VulnerabilityRecord("M002", Zone.CENTER, "协防", 61),
new VulnerabilityRecord("M002", Zone.CENTER, "站位", 82),
new VulnerabilityRecord("M003", Zone.PENALTY_AREA, "盯人", 44),
new VulnerabilityRecord("M003", Zone.LEFT_FLANK, "回追", 66)
);
System.out.println("========== 全部漏洞记录 ==========");
records.forEach(System.out::println);
// 2. 统计总漏洞次数
long total = records.size();
System.out.println("\n总漏洞次数: " + total);
// 3. 按区域统计出现次数
System.out.println("\n========== 各区域漏洞次数 ==========");
Map<Zone, Long> byZone = records.stream()
.collect(Collectors.groupingBy(
VulnerabilityRecord::getZone,
Collectors.counting()));
byZone.forEach((zone, count) ->
System.out.printf("%-12s : %d 次%n", zone.getDesc(), count));
// 4. 按漏洞类型统计
System.out.println("\n========== 各类型漏洞次数 ==========");
Map<String, Long> byType = records.stream()
.collect(Collectors.groupingBy(
VulnerabilityRecord::getType,
Collectors.counting()));
byType.forEach((type, count) ->
System.out.printf("%-8s : %d 次%n", type, count));
// 5. 按区域排序,找出漏洞最多的区域(TOP排名)
System.out.println("\n========== 漏洞区域排名(高→低)==========");
List<Map.Entry<Zone, Long>> ranking = byZone.entrySet().stream()
.sorted(Map.Entry.<Zone, Long>comparingByValue().reversed())
.collect(Collectors.toList());
int rank = 1;
for (Map.Entry<Zone, Long> e : ranking) {
System.out.printf("第%d名 %-12s : %d 次%n",
rank++, e.getKey().getDesc(), e.getValue());
}
// 6. 统计每场比赛的漏洞次数
System.out.println("\n========== 各场比赛漏洞次数 ==========");
Map<String, Long> byMatch = records.stream()
.collect(Collectors.groupingBy(
VulnerabilityRecord::getMatchId,
Collectors.counting()));
byMatch.forEach((matchId, count) ->
System.out.printf("%-6s : %d 次%n", matchId, count));
// 7. 找出漏洞次数最多的单个区域及具体记录
Zone topZone = ranking.get(0).getKey();
System.out.println("\n========== 漏洞最多的区域明细: "
+ topZone.getDesc() + " ==========");
records.stream()
.filter(r -> r.getZone() == topZone)
.forEach(r -> System.out.println(" " + r));
}
}
运行结果
========== 全部漏洞记录 ==========
[M001] 左路-站位 (第12分钟)
[M001] 左路-回追 (第35分钟)
[M001] 中路-协防 (第58分钟)
[M001] 禁区-盯人 (第77分钟)
[M002] 左路-站位 (第8分钟)
[M002] 右路-盯人 (第23分钟)
[M002] 中路-协防 (第61分钟)
[M002] 中路-站位 (第82分钟)
[M003] 禁区-盯人 (第44分钟)
[M003] 左路-回追 (第66分钟)
总漏洞次数: 10
========== 各区域漏洞次数 ==========
左路 : 4 次
中路 : 3 次
禁区 : 2 次
右路 : 1 次
========== 各类型漏洞次数 ==========
盯人 : 3 次
协防 : 2 次
站位 : 3 次
回追 : 2 次
========== 漏洞区域排名(高→低)==========
第1名 左路 : 4 次
第2名 中路 : 3 次
第3名 禁区 : 2 次
第4名 右路 : 1 次
========== 各场比赛漏洞次数 ==========
M001 : 4 次
M002 : 4 次
M003 : 2 次
========== 漏洞最多的区域明细: 左路 ==========
[M001] 左路-站位 (第12分钟)
[M001] 左路-回追 (第35分钟)
[M002] 左路-站位 (第8分钟)
[M003] 左路-回追 (第66分钟)
核心知识点
| 需求 | 使用的 Java 技术 |
|---|---|
| 按区域统计次数 | Collectors.groupingBy + Collectors.counting() |
| 取前 N 名 / 排序 | Map.Entry.comparingByValue().reversed() |
| 分类汇总 | Stream 链式操作 |
| 枚举建模区域 | enum 提高可读性 |
扩展建议
如果要做真实统计系统,还可以增加:
- 时间维度统计:按上/下半场、15分钟区间统计
- 权重统计:禁区漏洞权重 > 中场漏洞
- 导出 Excel:用
EasyExcel或Apache POI - 数据库版:
SELECT zone, COUNT(*) FROM vulnerability GROUP BY zone
需要我把上面某个扩展(比如按时间区间统计、或接入数据库查询)也写出来吗?