Displaying a HashMap...

Discussion in 'Plugin Development' started by Whomp54, Dec 27, 2013.

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

    Whomp54

    Hello there, I was wondering how I could display a HashMap, but every new key inside the HashMap would go to another line. Is it possible?

    So basically if I didn't explain it good up there, This is basically what I want to do.

    -Add players to a HashMap
    -Get the HashMap items
    -Organize the HashMap items so that there is one per line on the chat.

    Sorry, i have no code that would be of use :(
     
  2. Offline

    Freelix2000

    The easiest (And maybe the only) way to do this is with a loop. I haven't tested this code, so it may not work perfectly, but hopefully you will get the idea.
    Code:java
    1. HashMap<Player, String> HM = new HashMap<Player, String>();
    2. Set<Player> keys = HM.keySet();
    3. while(keys.iterator().hasNext()){
    4. Player next = keys.iterator().next();
    5. p.sendMessage(next.getName() + " " + HM.get(next));
    6. keys.remove(next);
    7. //Removes key from list so the loop will stop
    8. }
     
  3. Offline

    NathanWolf

    You can also use the fancy for syntax, like

    Code:
    for (String key : map.keySet()) {
      Bukkit.broadcastMessage(key);
    }
    
     
  4. Offline

    1Rogue Retired Staff

    Code:java
    1. StringBuilder sb = new StringBuilder();
    2. for (Entry<K, V> entry : yourMap) {
    3. sb.append(K.toString()).append(" - ").append(V.toString()).append('\n');
    4. }
    5. Bukkit.broadcastMessage(sb.toString());
     
  5. Offline

    Whomp54

    Ok, I'll try some of those.

    @Freelix2000 I'm acutally using HashMap<String, Integer>, and it says at this line: "p.sendMessage(next.toString() + " " + keys.get(next));", that the method get(String) is undefined for the type Set<String>. What should I do?​

    1Rogue Yours didn't work either. I think it might have to do with the fact i'm using <String, Integer> again

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  6. Offline

    1Rogue Retired Staff


    Can you supply the code you used?
     
Thread Status:
Not open for further replies.

Share This Page