Looping over hashmap

Discussion in 'Spigot Plugin Development' started by Jonnyo101, Apr 29, 2017.

  1. Hello. Is this a good way to loop over a hashmap of this type.

    Code (Text):

    public static HashMap<String, List<RealmUpgrade>> realmUpgrades = new HashMap<>();

        public static void UpgradeBuilder() {

            if (runUpgrades) {

                realmUpgrades.values().forEach((player) -> {

                    player.forEach((realmUpgrades) -> {

                        if (realmUpgrades.isCancel()) {
                            player.remove(realmUpgrades);
                        }

                        realmUpgrades.PlaceBlock();

                    });

                });

            }

            Bukkit.getScheduler().scheduleSyncDelayedTask(Main.plugin, new Runnable() {
                @Override
                public void run() {
                    UpgradeBuilder();
                }
            }, upgradeDelay);

        }
     
  2. Code (Java):
    for (entry:realmUpgrades.entrySet()){
        //Do whatever
    }
     
  3. Dont remove an entry while looping through the collection.
     
  4. Can you not remove them when using .forEach ? i know you can not when using for()

    EDIT: Nope you can not i was told you can. Okay time for a change !
     
    #4 Jonnyo101, Apr 29, 2017
    Last edited: Apr 29, 2017