代码之家  ›  专栏  ›  技术社区  ›  Matias Diez

Java:如何根据属性值将对象合并到ARARYLIST中?

  •  1
  • Matias Diez  · 技术社区  · 6 年前

    我正在使用Spring Bug、Java和MySQL构建(或学习如何)一个运动REST API。我正在构建一个方法,该方法当前从匹配项集合中获取每个匹配项并返回 ArrayList 属于 TeamStandings 完整的匹配列表。

    方法如下:

    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": 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
            }
        ]
    

    3 回复  |  直到 6 年前
        1
  •  2
  •   Nikolas Charalambidis    6 年前

    遍历 TeamStanding 注意团队ID并执行添加。您可能希望使用映射将团队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[] ,然后您必须使用相同的思想,但是,将两个迭代组合在一起可能有点复杂。然后我建议您坚持这两个独立的迭代。与性能相比,简洁性、可读性和可维护性——此外,性能并不是真正的问题。

        2
  •  0
  •   Arun Chaudhary    6 年前

    您可以使用hashmap。使用“idteam”作为键,对象teamstanding作为值。现在您可以迭代结果列表,如果您在映射中找到了对象,只需更新其字段,如果没有找到,则插入对象。迭代完成后,可以调用map.values(),它将为您提供一个对象集合(teamstanding),然后可以使用该集合创建一个新的arraylist。

    代码如下:

    public List<TeamStanding> mergeTeamStandingList(List<TeamStanding> teamStandingList) {
        final Map<Integer, TeamStanding> idTeamVsTeamStandingMap = new HashMap<Integer, TeamStanding>();
        teamStandingList.forEach(teamStanding -> {
            if(idTeamVsTeamStandingMap.containsKey(teamStanding.getIdTeam())) {
                TeamStanding teamStanding1 = idTeamVsTeamStandingMap.get(teamStanding.getIdTeam());
                teamStanding1.setTeamDraws(teamStanding1.getTeamDraws() + teamStanding.getTeamDraws());
                //so on
            } else {
                idTeamVsTeamStandingMap.put(teamStanding.getIdTeam(), teamStanding);
            }
        });
    
        return new ArrayList<>(idTeamVsTeamStandingMap.values());
    }
    
        3
  •  0
  •   Nico Van Belle    6 年前

    在您的 Teamstanding

    public TeamStanding merge(TeamStanding other) {
         this.teamPoints += other.getTeamPoints();
         this.teamPlayed += other.getTeamPlayed();
         this.teamWins += other.getTeamWins();
         this.teamDraws += other.getTeamDraws();
         this.teamGoalsFavor += other.getTeamGoalsFavor();
         this.teamLoses += other.getTeamLoses();
         this.teamGoalDif += other.getTeamGoalDif();
         return this;
    }
    

    Map<Integer, Optional<TeamStanding>> mapReduced = teamStandings
    .stream()
    .collect(groupingBy(TeamStanding::getIdTeam, Collectors.reducing(TeamStanding::merge)));