HashMap comparing second values?

Discussion in 'Plugin Development' started by anonym110, Aug 16, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    anonym110

    Hey I am making a team plugin and I normally use Arrays for the teams, but I noticed HashMaps would be nicer as I only would need 1 HashMap for all teams, though now I am stuck on a autobalance funtion.

    Code:
        private HashMap<Player, String> players = new HashMap<Player, String>();
     
        public void divideTeams() {
            for (Player player : players.keySet()) {
                ??
                }
        }
    The strings should be "red" and "blue" and I would like it to check the amount of players that have the string set red and the amount of players that have the string set to blue. Then while going through always giving the next person the string that has less ppl assigned.

    Thats the way I did it with Arrays:
    Code:
    for (Player player : players) {
                        int blue = blueTeam.size();
                        int red = redTeam.size();
                        if (blue >= red) {
                            redTeam.add(player);
                        }
                        else if (red > blue) {
                            blueTeam.add(player);
                        }
     
  2. Offline

    nisovin

    Using two lists or sets is probably better than using a map in this situation. Just because you can use a single map doesn't mean you should, sometimes other options are better.
     
  3. Offline

    toothplck1

    Code:
    int red = 0;
     
    int blue = 0;
     
    for(String team: players.Values()){
     
            if(team.equals("red")){
                        red++;
            }
            if(team.equals("blue")){
                        blue++;
            }
    }
    if (blue >= red) {
            players.add(player, "red");
    } else if (red > blue) {
            players.add(player, "blue");
    }
    
     
Thread Status:
Not open for further replies.

Share This Page