HashSet inside a HashMap

Discussion in 'Plugin Development' started by mollekake, Nov 8, 2012.

Thread Status:
Not open for further replies.
  1. i'm working on something, and i want to store some data in a hashmap.
    i want to store it as <playername, hashset>, and the hashset is a list of stuff.
    but how would i go on to alter the hashset? is it possible?
    i've never done it like this before.
  2. Offline


    It is pretty simple. In this example, the HashSet is of Strings.
    1. HashMap<String, HashSet<String>> map = new HashMap<String, HashSet<String>>();
    2. ...
    4. //to add the tag "blue team" to a player:
    5. HashSet set = map.get(playerName);
    6. if(set == null)
    7. {
    8. //this player does not have an entry yet
    9. //so let's add one!
    10. set = new HashSet<String>());
    11. map.put(playerName, set);
    12. }
    13. set.add("blue team");
  3. so what if i want to remove strings from the set? but keep the rest of the strings?

    anyone know?

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


    I think you would have to use map.getValue to retrieve the HashSet, then do whatever operations you want with it.
  5. Offline


    It depends really what you want to do, but you can always call the remove() method on the set.
    2. set.remove("string");
Thread Status:
Not open for further replies.

Share This Page