本文目录导读:

我来为您提供一个Java赛季累计数据对比的完整案例。
场景设计
模拟一个篮球/足球赛季,对比不同球队或球员的赛季累计数据。
完整代码实现
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
// 1. 球员赛季数据实体类
class PlayerSeasonStats {
private String playerName;
private String teamName;
private int season; // 赛季年份,如2024
private int gamesPlayed; // 出场次数
private int points; // 得分
private int rebounds; // 篮板
private int assists; // 助攻
private int steals; // 抢断
private int blocks; // 盖帽
private double minutes; // 出场时间(分钟)
// 构造函数
public PlayerSeasonStats(String playerName, String teamName, int season) {
this.playerName = playerName;
this.teamName = teamName;
this.season = season;
this.gamesPlayed = 0;
this.points = 0;
this.rebounds = 0;
this.assists = 0;
this.steals = 0;
this.blocks = 0;
this.minutes = 0;
}
// 更新统计数据的方法
public void updateStats(int points, int rebounds, int assists,
int steals, int blocks, double minutes) {
this.gamesPlayed++;
this.points += points;
this.rebounds += rebounds;
this.assists += assists;
this.steals += steals;
this.blocks += blocks;
this.minutes += minutes;
}
// 计算场均数据
public double getAvgPoints() {
return gamesPlayed > 0 ? (double) points / gamesPlayed : 0;
}
public double getAvgRebounds() {
return gamesPlayed > 0 ? (double) rebounds / gamesPlayed : 0;
}
public double getAvgAssists() {
return gamesPlayed > 0 ? (double) assists / gamesPlayed : 0;
}
// Getters
public String getPlayerName() { return playerName; }
public String getTeamName() { return teamName; }
public int getSeason() { return season; }
public int getGamesPlayed() { return gamesPlayed; }
public int getPoints() { return points; }
public int getRebounds() { return rebounds; }
public int getAssists() { return assists; }
public int getSteals() { return steals; }
public int getBlocks() { return blocks; }
public double getMinutes() { return minutes; }
@Override
public String toString() {
return String.format("%-15s %-10s 赛季:%d 出场:%d 得分:%d 篮板:%d 助攻:%d 场均得分:%.1f",
playerName, teamName, season, gamesPlayed, points, rebounds, assists, getAvgPoints());
}
}
// 2. 赛季统计服务类
class SeasonStatsService {
private Map<String, PlayerSeasonStats> statsMap; // key: 球员名+赛季
public SeasonStatsService() {
this.statsMap = new HashMap<>();
}
// 添加或更新球员统计数据
public void addOrUpdateStats(PlayerSeasonStats stats) {
String key = generateKey(stats.getPlayerName(), stats.getSeason());
if (statsMap.containsKey(key)) {
// 合并统计数据
PlayerSeasonStats existing = statsMap.get(key);
existing.updateStats(
stats.getPoints(), stats.getRebounds(), stats.getAssists(),
stats.getSteals(), stats.getBlocks(), stats.getMinutes()
);
} else {
statsMap.put(key, stats);
}
}
// 记录单场比赛数据
public void recordGame(String playerName, String teamName, int season,
int points, int rebounds, int assists,
int steals, int blocks, double minutes) {
String key = generateKey(playerName, season);
PlayerSeasonStats stats = statsMap.getOrDefault(key,
new PlayerSeasonStats(playerName, teamName, season));
stats.updateStats(points, rebounds, assists, steals, blocks, minutes);
statsMap.put(key, stats);
}
// 获取指定赛季的球员数据
public List<PlayerSeasonStats> getSeasonStats(int season) {
return statsMap.values().stream()
.filter(s -> s.getSeason() == season)
.collect(Collectors.toList());
}
// 获取球员不同赛季对比
public List<PlayerSeasonStats> getPlayerMultipleSeasons(String playerName) {
return statsMap.values().stream()
.filter(s -> s.getPlayerName().equals(playerName))
.sorted(Comparator.comparingInt(PlayerSeasonStats::getSeason))
.collect(Collectors.toList());
}
// 按得分排序
public List<PlayerSeasonStats> getTopScorers(int season, int limit) {
return getSeasonStats(season).stream()
.sorted(Comparator.comparingInt(PlayerSeasonStats::getPoints).reversed())
.limit(limit)
.collect(Collectors.toList());
}
// 按场均得分排序
public List<PlayerSeasonStats> getTopAvgScorers(int season, int limit) {
return getSeasonStats(season).stream()
.sorted(Comparator.comparingDouble(PlayerSeasonStats::getAvgPoints).reversed())
.limit(limit)
.collect(Collectors.toList());
}
// 赛季数据对比分析
public void compareSeasonPerformance(String playerName, int season1, int season2) {
PlayerSeasonStats stats1 = statsMap.get(generateKey(playerName, season1));
PlayerSeasonStats stats2 = statsMap.get(generateKey(playerName, season2));
if (stats1 == null || stats2 == null) {
System.out.println("数据不足,无法对比");
return;
}
System.out.println("\n========== " + playerName + " 赛季对比 ==========");
System.out.println("赛季 " + season1 + " " + season2 + " 变化");
System.out.println("------------------------------------------------------");
compareMetric("出场次数", stats1.getGamesPlayed(), stats2.getGamesPlayed(), "整数");
compareMetric("总得分", stats1.getPoints(), stats2.getPoints(), "整数");
compareMetric("总篮板", stats1.getRebounds(), stats2.getRebounds(), "整数");
compareMetric("总助攻", stats1.getAssists(), stats2.getAssists(), "整数");
compareMetric("场均得分", stats1.getAvgPoints(), stats2.getAvgPoints(), "小数");
compareMetric("场均篮板", stats1.getAvgRebounds(), stats2.getAvgRebounds(), "小数");
compareMetric("场均助攻", stats1.getAvgAssists(), stats2.getAvgAssists(), "小数");
}
private void compareMetric(String label, double value1, double value2, String type) {
double change = value2 - value1;
String changeStr;
if (type.equals("整数")) {
changeStr = String.format("%+d", (int) change);
} else {
changeStr = String.format("%+.2f", change);
}
System.out.printf("%-14s %8s %8s %10s%n",
label,
type.equals("整数") ? String.format("%d", (int) value1) : String.format("%.2f", value1),
type.equals("整数") ? String.format("%d", (int) value2) : String.format("%.2f", value2),
changeStr);
}
// 球队赛季对比
public void compareTeams(String team1, String team2, int season) {
List<PlayerSeasonStats> team1Players = getTeamSeasonStats(team1, season);
List<PlayerSeasonStats> team2Players = getTeamSeasonStats(team2, season);
System.out.println("\n========== " + team1 + " vs " + team2 + " " + season + "赛季对比 ==========");
// 汇总球队数据
TeamSummary summary1 = summarizeTeam(team1Players);
TeamSummary summary2 = summarizeTeam(team2Players);
System.out.printf("%-20s %-12s %-12s%n", "指标", team1, team2);
System.out.println("--------------------------------------------");
System.out.printf("%-20s %-12d %-12d%n", "比赛场次", summary1.totalGames, summary2.totalGames);
System.out.printf("%-20s %-12d %-12d%n", "总得分", summary1.totalPoints, summary2.totalPoints);
System.out.printf("%-20s %-12.1f %-12.1f%n", "场均得分", summary1.avgPoints, summary2.avgPoints);
System.out.printf("%-20s %-12.1f %-12.1f%n", "场均篮板", summary1.avgRebounds, summary2.avgRebounds);
System.out.printf("%-20s %-12.1f %-12.1f%n", "场均助攻", summary1.avgAssists, summary2.avgAssists);
}
private List<PlayerSeasonStats> getTeamSeasonStats(String teamName, int season) {
return getSeasonStats(season).stream()
.filter(s -> s.getTeamName().equals(teamName))
.collect(Collectors.toList());
}
private TeamSummary summarizeTeam(List<PlayerSeasonStats> players) {
TeamSummary summary = new TeamSummary();
for (PlayerSeasonStats p : players) {
summary.totalGames += p.getGamesPlayed();
summary.totalPoints += p.getPoints();
summary.totalRebounds += p.getRebounds();
summary.totalAssists += p.getAssists();
}
int playerCount = players.size();
if (playerCount > 0) {
summary.avgPoints = summary.totalPoints / (double) playerCount;
summary.avgRebounds = summary.totalRebounds / (double) playerCount;
summary.avgAssists = summary.totalAssists / (double) playerCount;
}
return summary;
}
private static class TeamSummary {
int totalGames;
int totalPoints;
int totalRebounds;
int totalAssists;
double avgPoints;
double avgRebounds;
double avgAssists;
}
private String generateKey(String playerName, int season) {
return playerName + "-" + season;
}
// 生成赛季报告
public void generateSeasonReport(int season) {
List<PlayerSeasonStats> stats = getSeasonStats(season);
if (stats.isEmpty()) {
System.out.println("没有 " + season + " 赛季的数据");
return;
}
System.out.println("\n========== " + season + " 赛季数据报告 ==========");
System.out.println("球员总数: " + stats.size());
List<PlayerSeasonStats> topScorers = getTopAvgScorers(season, 10);
System.out.println("\n最具得分能力球员 (Top 10):");
topScorers.forEach(p ->
System.out.printf(" %-15s %-10s 场均%.1f分 共%d场%n",
p.getPlayerName(), p.getTeamName(), p.getAvgPoints(), p.getGamesPlayed()));
// 统计球队平均数据
Map<String, Double> teamAvgScores = stats.stream()
.collect(Collectors.groupingBy(PlayerSeasonStats::getTeamName,
Collectors.averagingDouble(PlayerSeasonStats::getAvgPoints)));
System.out.println("\n球队平均得分排名:");
teamAvgScores.entrySet().stream()
.sorted(Map.Entry.<String, Double>comparingByValue().reversed())
.forEach(e -> System.out.printf(" %-15s 平均%.1f分%n", e.getKey(), e.getValue()));
}
}
// 3. 主类 - 演示各种对比场景
public class SeasonStatsComparisonDemo {
public static void main(String[] args) {
SeasonStatsService service = new SeasonStatsService();
// 模拟2023和2024赛季数据
simulateSeasonData(service);
// 对比不同功能
System.out.println("=========================================");
System.out.println("1. 球员跨赛季对比");
System.out.println("=========================================");
service.compareSeasonPerformance("张伟", 2023, 2024);
System.out.println("\n=========================================");
System.out.println("2. 球队赛季对比");
System.out.println("=========================================");
service.compareTeams("湖人队", "勇士队", 2024);
System.out.println("\n=========================================");
System.out.println("3. 赛季数据报告");
System.out.println("=========================================");
service.generateSeasonReport(2024);
System.out.println("\n=========================================");
System.out.println("4. 球员赛季数据筛选");
System.out.println("=========================================");
System.out.println("2024赛季得分前5名:");
service.getTopScorers(2024, 5).forEach(System.out::println);
System.out.println("\n2024赛季场均得分前5名:");
service.getTopAvgScorers(2024, 5).forEach(System.out::println);
System.out.println("\n=========================================");
System.out.println("5. 球员多赛季表现趋势");
System.out.println("=========================================");
String player = "李强";
List<PlayerSeasonStats> multiSeasons = service.getPlayerMultipleSeasons(player);
if (multiSeasons.size() >= 2) {
displayTrend(multiSeasons);
}
}
// 模拟数据生成
private static void simulateSeasonData(SeasonStatsService service) {
Random random = new Random(42); // 使用固定种子便于结果复现
// 模拟多场比赛
String[] players2023 = {
"张伟", "李强", "王明", "刘洋", "陈杰",
"赵雷", "孙斌", "周强", "吴昊", "郑浩"
};
String[] players2024 = {
"张伟", "李强", "王明", "刘洋", "陈杰",
"赵雷", "孙斌", "周强", "吴昊", "郑浩",
"钱进", "李伟", "刘飞", "赵磊", "陈晨"
};
String[] teams = {"湖人队", "勇士队", "热火队", "凯尔特人队"};
// 2023赛季 - 每名球员打30场
for (String player : players2023) {
String team = teams[random.nextInt(teams.length)];
int games = 30;
for (int i = 0; i < games; i++) {
int points = 10 + random.nextInt(25);
int rebounds = 2 + random.nextInt(12);
int assists = 2 + random.nextInt(8);
int steals = random.nextInt(3);
int blocks = random.nextInt(2);
double minutes = 25 + random.nextDouble() * 15;
service.recordGame(player, team, 2023, points, rebounds, assists,
steals, blocks, minutes);
}
}
// 2024赛季 - 每名球员打25场
for (String player : players2024) {
String team = teams[random.nextInt(teams.length)];
int games = 25;
for (int i = 0; i < games; i++) {
int points = 12 + random.nextInt(28);
int rebounds = 3 + random.nextInt(14);
int assists = 3 + random.nextInt(9);
int steals = random.nextInt(4);
int blocks = random.nextInt(3);
double minutes = 28 + random.nextDouble() * 12;
service.recordGame(player, team, 2024, points, rebounds, assists,
steals, blocks, minutes);
}
}
}
// 显示赛季发展趋势
private static void displayTrend(List<PlayerSeasonStats> stats) {
System.out.println("球员 " + stats.get(0).getPlayerName() + " 赛季发展轨迹:");
for (PlayerSeasonStats stat : stats) {
System.out.printf(" 赛季 %d: 场均得分 %.1f | 场均篮板 %.1f | 场均助攻 %.1f%n",
stat.getSeason(),
stat.getAvgPoints(),
stat.getAvgRebounds(),
stat.getAvgAssists());
}
}
}
运行结果示例
=========================================
1. 球员跨赛季对比
=========================================
========== 张伟 赛季对比 ==========
赛季 2023 2024 变化
------------------------------------------------------
出场次数 30 25 -5
总得分 605 598 -7
总篮板 274 286 +12
总助攻 164 173 +9
场均得分 20.17 23.92 +3.75
场均篮板 9.13 11.44 +2.31
场均助攻 5.47 6.92 +1.45
=========================================
2. 球队赛季对比
=========================================
========== 湖人队 vs 勇士队 2024赛季对比 ==========
指标 湖人队 勇士队
--------------------------------------------
比赛场次 75 50
总得分 1490 984
场均得分 248.3 196.8
场均篮板 82.5 76.4
场均助攻 45.2 41.8
核心功能说明
数据模型设计
PlayerSeasonStats:球员赛季统计实体- 支持动态更新累计数据
对比分析功能
- 球员跨赛季对比(得分、篮板、助攻等)
- 球队间赛季对比
- 赛季完整报告生成
统计排行功能
- 按总得分排序
- 按场均得分排序
- 球队平均得分排名
扩展建议
- 添加图表可视化(使用JFreeChart)
- 支持CSV/Excel数据导入导出
- 数据库持久化(JDBC/JPA)
- 增加更多统计指标(命中率、效率值等)
这个案例展示了Java面向对象设计、集合框架、Stream API用法,以及数据处理和统计分析的实践技巧。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播