gpt4 book ai didi

JAVA:如何根据属性值合并 arrayList 中的对象?

转载 作者:行者123 更新时间:2023-11-30 02:05:54 25 4
gpt4 key购买 nike

我正在使用 Spring Boot、Java 和 MySQL 构建(或学习如何)体育 REST API。我正在构建一个方法,该方法当前从比赛集合中获取每场比赛,并返回 TeamStandingsArrayList 以获取完整的比赛列表。

方法如下:

public List<TeamStanding> createStandingsTable(Match[] matches){
List<TeamStanding> teamStandings = new ArrayList<TeamStanding>();
for(int i = 0;i < matches.length; i++) {
TeamStanding firstTeam = new TeamStanding();
TeamStanding secondTeam = new TeamStanding();

//set team ids
firstTeam.setIdTeam(matches[i].getWcmHome());
secondTeam.setIdTeam(matches[i].getWcmAway());


//first team stats
firstTeam.setTeamPlayed((long) 1);
firstTeam.setTeamGoalsFavor(matches[i].getWcmHomeGoals());
firstTeam.setTeamGoalsAgainst(matches[i].getWcmAwayGoals());
firstTeam.setTeamGoalDif(firstTeam.getTeamGoalsFavor() - firstTeam.getTeamGoalsAgainst());

//second team stats
secondTeam.setTeamPlayed((long) 1);
secondTeam.setTeamGoalsFavor(matches[i].getWcmAwayGoals());
secondTeam.setTeamGoalsAgainst(matches[i].getWcmHomeGoals());
secondTeam.setTeamGoalDif(secondTeam.getTeamGoalsFavor() - secondTeam.getTeamGoalsAgainst());

//combined team stats

if(firstTeam.getTeamGoalsFavor() > secondTeam.getTeamGoalsFavor()) {
firstTeam.setTeamWins((long) 1);
firstTeam.setTeamLoses((long) 0);
firstTeam.setTeamDraws((long) 0);
firstTeam.setTeamPoints((long) 3);
secondTeam.setTeamWins((long) 0);
secondTeam.setTeamLoses((long) 1);
secondTeam.setTeamDraws((long) 0);
secondTeam.setTeamPoints((long) 0);
} else if (firstTeam.getTeamGoalsFavor() == secondTeam.getTeamGoalsFavor()) {
firstTeam.setTeamWins((long) 0);
firstTeam.setTeamLoses((long) 0);
firstTeam.setTeamDraws((long) 1);
firstTeam.setTeamPoints((long) 1);
secondTeam.setTeamWins((long) 0);
secondTeam.setTeamLoses((long) 0);
secondTeam.setTeamDraws((long) 1);
secondTeam.setTeamPoints((long) 1);
} else {
firstTeam.setTeamWins((long) 0);
firstTeam.setTeamLoses((long) 1);
firstTeam.setTeamDraws((long) 0);
firstTeam.setTeamPoints((long) 0);
secondTeam.setTeamWins((long) 1);
secondTeam.setTeamLoses((long) 0);
secondTeam.setTeamDraws((long) 0);
secondTeam.setTeamPoints((long) 3);
}
teamStandings.add(firstTeam);
teamStandings.add(secondTeam);
}
return teamStandings;
}

结果是这样的:

[
{
"idTeam": 7,
"teamPoints": 3,
"teamPlayed": 1,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 0,
"teamGoalsFavor": 4,
"teamGoalsAgainst": 1,
"teamGoalDif": 3
},
{
"idTeam": 13,
"teamPoints": 0,
"teamPlayed": 1,
"teamWins": 0,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 4,
"teamGoalDif": -3
},
{
"idTeam": 4,
"teamPoints": 3,
"teamPlayed": 1,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 0,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 0,
"teamGoalDif": 1
},
{
"idTeam": 7,
"teamPoints": 0,
"teamPlayed": 1,
"teamWins": 0,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 0,
"teamGoalsAgainst": 1,
"teamGoalDif": -1
}
]

我的问题是如何基于idTeam合并这些对象?我试图实现的结果是在 idTeam 保持不变的情况下将所有其余属性相加。在给定的示例中,预期的结果是:

[
{
"idTeam": 7,
"teamPoints": 3,
"teamPlayed": 2,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 4,
"teamGoalsAgainst": 2,
"teamGoalDif": 2
},
{
"idTeam": 13,
"teamPoints": 0,
"teamPlayed": 1,
"teamWins": 0,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 4,
"teamGoalDif": -3
},
{
"idTeam": 4,
"teamPoints": 3,
"teamPlayed": 1,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 0,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 0,
"teamGoalDif": 1
}
]

还有一个细节,我首先构建了 TeamStandingsArrayList,现在我正在尝试合并它们,但也许我应该将它们堆叠为一个循环匹配数组,在上面相同的方法中,但我不确定。

最佳答案

迭代 TeamStanding 的列表,记下团队 ID 并执行添加。您可能希望使用 Map 将一对团队 ID 保存为键,将团队本身保存为值,以便于操作。这是截图(我还没有测试过,所以你可能需要稍微修改一下)。

List<TeamStanding> list = createStandingsTable(matches);
Map<Integer, TeamStanding> map = new HashMap<>();

for (TeamStanding team: list) {
int id = team.getIdTeam();
if (map.containsKey(id)) {
TeamStanding other = map.get(id);
other.setTeamPoints(team.getTeamPoints());
other.setTeamPlayed(team.getTeamPlayed());
// and so on...
} else {
map.put(id, team);
}
}

List<TeamStanding> merged = new ArrayList<>(map.values());

如果您想创建合并的List<TeamStanding>直接来自Match[] ,那么你必须使用相同的想法,但是,将两个迭代组合在一起可能有点复杂。然后我建议您坚持这两个单独的迭代。简洁性、可读性和可维护性高于性能 - 而且,性能在这里并不是真正的问题。

关于JAVA:如何根据属性值合并 arrayList 中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51387128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com