HashMap problem

Discussion in 'Spigot Plugin Development' started by Durick, Jun 12, 2019 at 9:31 PM.

  1. I would really appreciate if you put the code in code tags instead of paste. Makes it a lot easier to look over especially when you're splitting it up

    Also can you explain in simple terms what you're trying to do?
  2. Hello / Salut l'ami!

    It looks like you wants to add a map into a map into a map.. I really recommand you to create a wrapper class instead:
    final Map<UUID, MyWrapper> list = new HashMap<>();
  3. Salut,

    C'est plus facile pour moi de parler en francais. Alors déjà je ne sais pas comment utiliser les balises pour mettre mon code, désolé. Ensuite, je voudrais te demander comment je pourrais créer une classe "Wrapper" pour qu'elle soit utile pour mon code.
    It's easier for me to speak French. So already I do not know how to use the tags to put my code, sorry. Then I would like to ask you how I could create a class "Wrapper" so that it is useful for my code.
  4. Take a look into encapsulation. It will seem handy, not only for now but for later on as well.
  5. Code (Text):

        public static HashMap<Player, HashMap<Integer, HashMap<ItemStack, Integer>>> list = new HashMap<Player, HashMap<Integer, HashMap<ItemStack, Integer>>>();

    public static void setItem(Player p) {
            if(list.containsKey(p)) {
                for(Entry<ItemStack, Integer> i : list.get(p).entrySet()) {
                    ItemStack item = new ItemStack(i.getKey());

    I just did some code where I had a singular, nested HashMap. I am assuming you are trying to access / manipulate a nested HashMap properly.
    What I did to access the HashMap was string 2 get()s together.

    So if you wanted to get the value in the nested HashMap<ItemStack, Integer> you would do list.get(Player).get(Integer).get(ItemStack).
    Probably not the best coding practice but it seemed more logical and easier for me to understand.

    Make sure to add a test to make sure that the key of the Integer or ItemStack exists, otherwise you will get a NullPointerException and can crash the server.
    Example from my code:
    Code (Text):

                if (!(playerToHomes.containsKey(playerName))) {
                    playerToHomes.put(playerName, homesToLocation /*homesToLocation is a nested HashMap*/);

    More code examples from my plugin:
    Code (Text):

        public static void setHomesDeserialization() {
            for (String playerKey : Configs.getSetHomeConfig().getConfigurationSection("players").getKeys(false)) {
            //Running for every player in the config
                for (String homeKey : Configs.getSetHomeConfig().getConfigurationSection("players." + playerKey + ".homes").getKeys(false)) {
                //Running for every home of a player
                    Location homeLocation = (Location) Configs.getSetHomeConfig().get("players." + playerKey + ".homes." + homeKey);
                    homesToLocation.put(homeKey, homeLocation);
                playerToHomes.put(playerKey, homesToLocation);

    Edit: So after reading your code again, I am thinking something like this might be what you are trying to do:
    Code (Text):

    public static HashMap<Player, HashMap<Integer, HashMap<ItemStack, Integer>>> list = new HashMap<Player, HashMap<Integer, HashMap<ItemStack, Integer>>>();
    static HashMap<ItemStack, Integer> container = new HashMap<ItemStack, Integer>();
    static HashMap<Integer, HashMap<ItemStack, Integer>> container2 = new HashMap<Integer, HashMap<ItemStack, Integer>>();
    public static void setItem(Player p) {
        if (list.containsKey(p)) {
            for(Player playerKey : list.keySet()) {
                //This loop runs for every player.
                for(Integer integerKey : list.get(playerKey).keySet()) {
                    //For every Integer mapped to a HashMap, assuming every player has multiple Integers mapped to them
                    for(ItemStack is : list.get(playerKey).get(integerKey).keySet() ) {
                        //For every ItemStack mapped to an Integer, assuming every player's integer has multiple ItemStacks mapped to it.
                        ItemStack item = new ItemStack(is);
                        item.setAmount(list.get(p).get(integerKey).get(is)); //Double check this like! I'm assuming this is what you're wanting to do!
                        container.put(item, list.get(playerKey).get(integerKey).get(is));
                    container2.put(integerKey, container);
                list.put(playerKey, container2);
    And again, I'm new to this myself so that code probably contains some terrible coding practices. Any more experienced developers wanna give some input?
    #6 Demonbarrage, Jun 13, 2019 at 4:48 AM
    Last edited: Jun 13, 2019 at 6:06 AM
  6. Please make an object here, if you need 3 hashmaps inside each other there's a definite need for an object. It will simplify and generally improve performance on all this code.

    Then just have a Map<UUID, YourObject>
    • Agree Agree x 3
  7. Un français =D (je vais quand même parler en anglais pour que tout le monde comprenne)

    You seems not know OOP ("how I could create a class ...") but it's very important for most of languages. So, I suggest you to learn it before creating plugins. In french, you have Graven - Development (Youtube, Discord) and in English StackOverflow, other youtubers ...

Share This Page