HashMap To Arraylist?

Discussion in 'Plugin Development' started by 8thDimension, Aug 15, 2011.

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

    8thDimension

    I have a HashMap with player names and each name is assigned a value. I have a command that adds and removes players from the hashmap but now I want a command that lists the players on the hashmap. Here's an example I want:

    "PlayerA" has the value 5
    "PlayerB" has the value 6
    "PlayerC" has the value 3

    When a player types a command it will list the player names and the values like such:

    PlayerA (5), PlayerB (6), PlayerC (3)

    Or something similar to that. How would I achieve this? Would I need even need an array list for that?

    Thanks in advance :D
     
  2. Offline

    Shamebot

    Code:java
    1. HashMap<String,Integer> map = ... ;
    2. StringBuilder builder = new StringBuilder();
    3. for(Entry<String,Integer> entry : map.entrySet)
    4. {
    5. builder.append(entry.getKey()).append(" (").append(entry.getValue()).append("), ");
    6. }
     
  3. Offline

    8thDimension

    Thank you very much :D
     
  4. Offline

    8thDimension

    Hmm strange... When I do that it seems to duplicate the last item added...

    What happens is when a player types a certain command it adds to the hashmap and when they type another command it will display the hashmap.. But it appears to be duplicating the added item when I try showing the key's/values of the hashmap :\
     
  5. Offline

    K900

    Maybe you're creating a StringBuilder globally? You need to recreate it every time. Also, can you show me the code?
     
Thread Status:
Not open for further replies.

Share This Page