HashMap subMap ?

Discussion in 'Plugin Development' started by BugsyFTW, Aug 17, 2013.

Thread Status:
Not open for further replies.
  1. Hey Bukkit, i'm here having some difficulties, i'm trying to divide an HashMap of players into 2 teams(different hashmaps), i used this method using ArrayList's but in this case i need to use HashMap.

    So i have these 3 HashMaps:

    Code:java
    1. public static Map<Game, String> in_lobby = new HashMap<Game, String>();
    2. public static Map<Game, String> on_red = new HashMap<Game, String>();
    3. public static Map<Game, String> on_blue = new HashMap<Game, String>();


    I want to divide the in_lobby HashMap into the red team and the blue team, i have this code, but subList's are only for ArrayList's, so how would i do it with the HashMap ?

    Code:java
    1. if(in_lobby.size() > 0){
    2. on_red.putAll(in_lobby.subList(0, in_lobby.size() / 2 + in_lobby.size()%2));
    3. on_blue.putAll(in_lobby.subList(in_lobby.size() / 2 + in_lobby.size()%2, in_lobby.size()));
    4. in_lobby.clear();
    5. }
     
  2. BugsyFTW Why not add them first to a list, divide that list in 2, and then add them to the hashmap they belong to?
     
  3. Offline

    Lactem

    Why not make Game contain a method called getTeam() that returns a team enum? Then you can loop through the map and check if the player is on red team or blue team.
     
  4. This is before the game starts, it's shuffling and then diving the players into 2 even teams
     
  5. BugsyFTW Just put all the players in a list, divide the list in 2, put one half in the on_red map and the other galf in the on_blue map.
     
  6. Offline

    Lactem

    Loop through and add every other player to one list and the rest to the other?
     
  7. BugsyFTW Try this:

    for(Player player : Bukkit.getOnlinePlayers()){
    boolean b = false;

    if(b){
    //put in red
    b = false;
    }else{
    //put in blue
    b = true;
    }
    }
     
Thread Status:
Not open for further replies.

Share This Page