Good way to loop over a hashmap ?

Discussion in 'BungeeCord 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. Opps wrong section..............
     
  3. WAS

    WAS

    If you are removing from within the iteration it's best to use a Entry from what I've dealt with. It really comes down to what you are doing with your looping/iteration. I'm not too familiar with Java 8's Lambda forEach, it may be initiating a Map.Entry.
     
  4. .forEach(new BiConsumer...
     
  5. WAS

    WAS

    Explain your post. That doesn't provide any usable information... If you are going to show code, post the docs or show how to actually use it.
     
  6. simpleauthority

    Supporter

    He means this:
    Code (Java):
    // I have a nice map with keys and values!
    Map<String, String> test = new HashMap<>();
    test.put("test", "value");
    test.put("value", "test");

    // I want to iterate it with Streams!
    test.forEach((key, value) -> {
        System.out.println("I am the key! \"" + key + "\"");
        System.out.println("I am the value! \"" + value + "\"");
    });
    A BiConsumer is a consumer that takes two inputs as opposed to a regular consumer that takes one. What two inputs could there be in a map? Ah, the key...and the value.

    forEach on a map takes in a BiConsumer argument.
     
    • Like Like x 1
  7. WAS

    WAS

    I know. I'm more referring to a lot of the Java 8 guys just throwing out a single Java 8 method like everyone knows and and this person is stupid for not knowing.

    New methods take getting used to. PHP used to be really bad with this back with PHP3-5

     
  8. WAS

    WAS

    Are you even aware what the years between these releases and catching up? Over a decade.

    Not to mention Java has one the worse rep out there for developers slowly adapting. Proof is plainly seen in the Spigot API and all over the forums and every Java website.
     
  9. simpleauthority

    Supporter

    Please. At this point you're kind of defending a very moot point. I replied to your post explaining the meaning of BiConsumer because you asked, and that's it. There's no point to continuing to argue about why people aren't used to using it or anything stupid like that. Heck, some people haven't touched 8's API features, because they haven't needed to. Likewise, some have used so many of 8's API features that don't exist in 7 that it's hard to see anyone not using them. It's just useless to argue about, so let's not.
     
    • Winner Winner x 1
  10. WAS

    WAS

    While you you just reiterate and explain what I've said. Lol Good job.
     
  11. Lol a decade so I have to explain all the Java 6 and 7 methods too? If you're too lazy to Google that's your problem.
     
  12. WAS

    WAS

    It's already been explained for you... You must just not be paying attention. Fact is you are another one of the people previously mentioned.
     
  13. What's been explained? That everyone has to explain Java 8 methods to you because you're too lazy to keep updated with the language?
    Clearly you're part of the problem then
     
    • Agree Agree x 1
  14. Why has nobody suggested this yet? Sometimes I feel like people on Spigot are trying to be too advanced. It's so simple...

    Code (Text):
    map.keySet(); // Loops over the keys
    map.values(); // Loops over the values
    For some explanation if needed:

    Code (Text):
    Map<Integer, String> test = new HashMap<Integer, String>();
    for(Integer id : test.keySet()){
        String value = test.get(id);
    }
     
    • Like Like x 2
    • Agree Agree x 1
  15. WAS

    WAS

    I know exactly what it does. As previously stated. Clearly, again, you aren't paying attention. There's a OP here, asking for help, and posting random incomplete broken code with no source or explaination is useless. What is so hard to understand.

    The user shouldn't have to google, or you are not helping, or supplying an answer to the question.
    Even if it seems apparent to you, you should assume they couldn't find their answer. Even if they didn't Google.
     
    • Winner Winner x 1
  16. Clearly there is some inconsistency in your answer.

    Nor should the user have to think for themselves or write code am I right? Let's provide everything, because that will surely help the person. There's nothing wrong with telling someone to google something, especially if it's something that easily found.
     
  17. WAS

    WAS

    Lol keywords "not too". And in complete reference to it's source code, and if it's appropriate for his use. Holy shit.

    Again with the not paying attention crap.
     
    #19 WAS, May 2, 2017
    Last edited: May 2, 2017