简体   繁体   中英

How to use a Comparator recursively?

I have to rank the teams in the array "names" (below) in order of who won the most games. If two teams won the same amount of games then I have to compare the wins of the teams that beat them. The code I have so far is below.

The full problem statement is given here: http://www.cs.duke.edu/csed/newapt/tournamentrank.html

So I want to use the comparator recursively. How can the Comparator can have access to the original data? I tried creating a Team class that takes a variable of the same class for the team that beat it, but that clearly doesn't work. Stuck here, please help!

public class TournamentRanker implements Comparator<String>{

public class Team {
    String name;
    Integer wins;
    Team beatEm;
}

        //HOW TO make maps visible to comparator?
         public String[] rankTeams(String[] names, String[] lostTo) { 

             //map all teams to number of wins & to team that beat them
             ArrayList<String> teams = new ArrayList<String>();
             HashMap<String, Integer> Teamwins = new HashMap<String, Integer>();
                HashMap<String, String> Whobeat = new HashMap<String, String>();

             for(int x=0; x<names.length; x++)
             {
                 if(!teams.contains(names[x]))
                     teams.add(names[x]);

                 if(!Teamwins.containsKey(names[x]))
                     Teamwins.put(names[x], 0);
                 Whobeat.put(names[x],  lostTo[x]);
                 if(!Teamwins.containsKey(lostTo[x]) && !lostTo[x].equals(""))
                     Teamwins.put(lostTo[x], 0);
                 if(!lostTo[x].equals(""))
                     Teamwins.put(lostTo[x], (Teamwins.get(lostTo[x])+1));
             }
             for(String s: names)
             {
                 Integer wins = Teamwins.get(s);
                 Team beatEm = new Team(Whobeat.get(s), Teamwins.get(Whobeat.get(s)), ????)
             }
             //SORT list & turn into ARRAY
             Comparator<String> comp = new TournamentRanker();
             Collections.sort(teams, comp);
             String [] sortedTeams = new String[teams.size()];
             return teams.toArray(sortedTeams);



         }
        //NEED to use compareTo***?? OTHER strategy????

        //USE COMPARTOR - how to access all the data?

        public int compare(String team1, String team2){


        }       

 }

To make the maps visible, I suggest making the Comparator an inner class of TournamentRanker , and making the maps instance members of the TournamentRanker class, as follows:

public class TournamentRanker {

    public class Team {
        String name;
        Integer wins;
        Team beatEm;
    }

    // Map all teams to number of wins & to team that beat them
    ArrayList<String> teams = new ArrayList<String>();
    HashMap<String, Integer> Teamwins = new HashMap<String, Integer>();
    HashMap<String, String> Whobeat = new HashMap<String, String>();

    public String[] rankTeams(String[] names, String[] lostTo) { 
        TeamComparator teamComparator = new TeamComparator();

        // Use teamComparator to sort teams.
        ...
    }

    private TeamComparator implements Comparator<String> {
        public int compare(String team1, String team2){
            // This function can now access the maps.

            // Perform the comparison here.
            ...
        }
    }
}

If your goal is to write an object-oriented program, I would structure it as follows:

  1. A Game class containing references to the two teams that played the game, and each team's score.
  2. A Team class containing a Map of games played ( Game objects) indexed by opposing team ID. If two teams can meet more than once than you'll need a "multi-map" that allows more than one value object per key.
  3. A Tournament object containing both the Team instances (probably a Map indexed by team name) and Game objects (another Map indexed by some unique key of your choosing).

In your comparator, you can compare two teams' win-loss record, and in case of ties look at each team's individual games.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM