I'm tired of HashMaps

Discussion in 'Plugin Development' started by Jaker232, Dec 26, 2011.

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

    Jaker232

    HashMaps for player boolean checking is now suddenly boring to type. Most people have to say "Switch to ArrayList because it's easier". It is quite easy and I have been using it a few times which makes my code very clean.

    I'll never go back to HashMap for player boolean checking. Are you?
     
  2. Offline

    Kaikz

    What do you mean by player boolean checking?
     
  3. Offline

    Zeerix

    Just use HashSet
     
  4. "Player boolean checking"?
    This is the wrong use of HashMaps...Simple things like that should be Lists and Sets, depending on the specific need. HashMaps are when you have to store data with a player, such as a Number, Class, or other object
     
  5. Offline

    Afforess

    You should be using a HashSet for this, not an ArrayList. In fact, you get worse performance for .contains(...) calls on an arraylist...

    Methinks you need to brush up on data structures 101
     
    Brain, AlbireoX, Bone008 and 2 others like this.
  6. Offline

    Zeerix

    And structured programming 101.
    You can hide the dirty coding details in two methods (example: setPlayerTired(Player, boolean) and isPlayerTired(Player)) and most of your code doesn't have to touch the HashMaps at all. And you can easily change the data structure you are using for this.
     
    Brain likes this.
  7. Offline

    Jaker232

    How come some people tell me to use ArrayList, now you're telling me to go back? What the hell is this community type? Reversing?
     
  8. Offline

    Zeerix

    You didn't tell us about the use case. The exact class to use depends on that.
     
  9. Offline

    Vhab

    Is a hammer the most efficient tool for both nails and screws?
    Look up the 'golden hammer' anti-pattern.

    Also, it is possible to ask a question without taking snipes.
    Try not to treat people like shit when you ask them to help you...
     
  10. Offline

    Jaker232

    This thread is pointless with flaming trolls. Please lock this, someone.
     
  11. Offline

    Vhab

    You're the only one doing the flaming...
     
    Brain, r3Fuze, Father Of Time and 5 others like this.
  12. Offline

    ZNickq

    here we go, another round in trying to breach the shield of stupidity which surrounds you...

    First, all of the developers who answered are pretty known plugin developers, and anyone would understand that they were actually giving you useful tips, which you, just like in all your other threads, simply ignored, and accused them of trolling...

    Second, you accused them of being "flaming trolls"...obviously, just like java , you have absolutely no idea what you are talking about, so i'll help a little...a flamer is a person who tries to create flame wars, by insulting other people. None of the above devs did that. A troll is somebody who posts inflamatory or offtopic content on a forum, again, not the case, everyone obviously tried to help you. You seem tobe using those 2 terms as an insult, making you a flamer, but it just shows your lack of intelligence.

    And third..jeez jaker, don't you get bored of this? happens to all your posts, people try to help you, you accuse them of trolling or flaming instead of accepting that you code like shit, and the thread is locked... Seriously, i would've gotten bored by now!
     
    Brain, r3Fuze, Bone008 and 7 others like this.
  13. Offline

    Jaker232

    Can people STOP!?!?
     
  14. Offline

    Kaikz

    My mind is full of fuck. Seriously.
     
    Brain, r3Fuze, Sleaker and 3 others like this.
  15. Offline

    NuclearW

    No it isn't. It's full of people like @Vhab @Afforess @tips48 all trying to help you do better by suggesting a better method and further resources to look into.

    No, you've called out "troll!" and in response you've gotten fairly standard responses to people being wrongly accused. @Jaker232 I think you need to calm down just a bit and see that just because someone suggests a different way does not mean that they are calling you stupid. They might even go so far as to say that what you are doing is, goodness, wrong, but they aren't calling you an idiot. They hope to help and expect you to take their advice, and if you don't want that advice you should not complain about it when you get it.

    This thread is in the wrong place, however, so I have moved it.
     
    Brain, r3Fuze, tomjw64 and 1 other person like this.
  16. Offline

    Evenprime

    @Jaker232 :

    Every datastructure that's part of the "java.util" package has a usecase where it will perform better than all the alternatives. It is important to learn what they do and what they do good/bad:

    Lists = storing items in arbitrary order
    Sets = storing items, only unique entries
    Maps = storing key-value pairs, keys are unique

    and each has various subclasses for specific cases:

    LinkedLists = fast access, inserting and removing of items in the front and end, slow access to items in the middle
    ArrayLists = fast access everywhere, fast inserting/removing at the end, slow inserting/removing at the front, low memory usage
    aso, you get the idea

    Instead of listening to random people you should learn about these things. In your case, if all you want to store is if a player is in a certain group (e.g. sleeping), the data structure of choice would be "Set". A player is in the set of sleeping players or he isn't. There are three Set implementations: LinkedHashSet, HashSet and TreeSet. Because you don't want to sort the players (TreeSet) and don't care about the order in which the players are stored (LinkedHashSet), you should go with HashSet (as others have suggested). "set.add(player); set.contains(player); set.remove(player);" that's all you need.
     
  17. I apologize for commenting on your issue, in which you asked for comments about.
     
    Hanii Puppy, Brain, r3Fuze and 2 others like this.
  18. Hmm, I'm curious, what about stuff like blocks, I was using List<> but I guess that wouldn't be the most effective way, I just need to store and get all, not really searching in the list for specific blocks.... oh and eventually I'll change the stored class's stuff, like a type or data value... what's the best method for those ? :} I would think HashSet but I want to make sure :-?
     
  19. Okay, one comment: Telling you to go back? When did we tell you to go back? A HashSet isn't a HashMap...So maybe instead of the community reversing, you need to reverse your attitude and reconsidering your approach
     
  20. Offline

    bergerkiller

    @Digi A HashMap uses the .hashcode() function on the key that is used. Things like Strings and basic types like Integers specify hashcodes. If none is specified by the class type, you can assume that the default one is used, which is basically the pointer to the variable itself. In those cases the instance is used.

    A HashMap is ONLY useful for lookups or cases where you need to have only one value per key. For example, if you only want one property set per player. Example:
    Code:
    HashMap<Player, Integer> ids = new HashMap<Player, Integer>():
    ids.put(player, 12);
    ids.put(player, 35); //now the value is 35
    A list is useful for when you need to collection to stay ordered. For example, you need to execute a list of tasks in a strict order. If the list is always the same size, use an array.

    A hashset is useful for simple contains operations and to have only one of the same object in the collection at all times. If you want to prevent a collection to have multiple of the same object, the hashset is your man.

    Of course, you can also use combinations. For example, the chunks in minecraft. Since the list iteration speed (how fast a for loop runs through it) is higher, they used a list next to a map to store the chunks. This way they could quickly obtain a chunk when they knew the x and z coordinates through the map, but also quickly update all chunks using the list.

    A combination can also be used to have only one of the same in a list.
    Code:
    List<String> items = new ArrayList<String>();
    Set<String> itemcheck = new HashSet<String>();
    void add(String item) {
        if (itemcheck.add(item)) {
            items.add(item);
        }
    }
    void test() {
        add("a");
        add("b");
        add("a"); //won't get added
    }
    But: important to know. The HashMap needs a hashcode, and the calculation of this hashcode can take a while. This means that things like this:
    Code:
    for (String key : map.getKeys()) {
        Object value = map.get(key);
    }
    Is a very bad way to code. You need to do this in those cases:
    Code:
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        //use key and value here
    }
    But even this lookup is slower than that of the list, since it has to generate the entrySet.
     
    halley likes this.
  21. Oh, List has faster iteration speed than a HashSet/Map ? I guess I'll stick to that then xD

    Hmm, in that case, would it be faster to use a HashMap<String, CustomClass> and use containsKey() or just use List<CustomClass> and iterate through each until I find the String I need to identify what object I need ?
     
  22. Offline

    Sagacious_Zed Bukkit Docs

    @Digi
    if you need to look for one thing in the entire collection, you are searching, In which case a hashmap has much better searching behavior than a list. where hashmap can search in O(1) and list is O(n)
     
  23. I'm not talking about hashmap.containsKey() vs list.contains(), I'm talking about hashmap.cotainsKey() vs list iteration and comparisson because I need to check a string inside the stored class or outside as the key of the hahmap, and I was wondering if list iteration was that fast that would even beat hashmap's containsKey() method :}
     
  24. Offline

    Sagacious_Zed Bukkit Docs

    @Digi
    list.contains() is the same as list iteration and comparison to see if it exits.

    On that note, if an object took a long time to hash, and comparisons on a small list is fast, then it could talk longer check a HashMap. but in real world use it is unlikely to happen.
     
  25. Offline

    halley

    bergerkiller tried to summarize it. Did a good job, but this thing always seems to be confusing to some people.

    Remember way back in the dark ages of your early childhood, you may have seen this big book with thin pages, called a PHONE BOOK? Let's use that as an example to understand the differences between lists, sets and maps. The keywords I have highlighted will help you choose the right data structure.

    Question one: How many Smiths are in the phonebook? Answer: Use a List to store your phonebook data. Why? The items are already in a convenient or important order, and you want to maintain that. You will want to scan through the entries to find the first Smith (Albert), then you will want to go to the next Smith (Bill), and the next Smith (Frank B.), and the next Smith (Frank J.), until there are no more Smiths. Once you find the end of the Smiths, you are pretty sure you're done searching. Lists are good for those situations. Lists are good whenever the order of the entries helps your search system.

    Question two: Does Mary Danforth live in this area? Answer: Use a Set to store your phonebook data. Why? All you're interested is whether Mary is a member of the community. The presence or absence of her name is enough to satisfy you. Extraneous data is irrelevant for your purpose. You shouldn't want to run your finger down the pages, they could be in some arbitrary order, you want to jump right to her entry, if there is one. Sets don't search everything in the order they were added, they just jump to the answer in one or two hops, almost immediately, almost by magic. Very very fast. But they suck at keeping things in a particular ordering that you would care about.

    Question three: What is Frank J. Smith's phone number? Answer: Use a Map to store your phonebook data. Why? The important thing is that you're not searching a bunch of phone number values for something, you're searching for a specific name, and that name will have the phone number associated with it. You don't need to read all the names starting with Aberforth, John Q. to get there; you can jump right to the proper entry in the book directly, almost immediately, and then fetch the phone number assigned to your target's name. Like Sets, the order better not be important, because a Map really just uses a Set to store the entries by the key part, and those have some some extra space to store the values part.

    Okay, lastly, if you've used a phonebook at all, you've probably had a variety of questions on various occasions. If it were just the last two, then you could use a Map and check if Frank or Mary were in the phone book, or get their numbers. Easy enough. But if you really really must also answer questions that involve the order and membership of the entries on the pages of the book, then you will have to use something more exotic, like combining a List and a Map together, or use a TreeMap which does something like that for you. However, they come with a cost of additional memory considerations, and/or slightly slower membership checking.
     
    Bone008, Vhab, aPunch and 9 others like this.
  26. Offline

    Jaker232

    ahh I get it. I'm cooling down. I'll stop my trolling/flaming attitude.

    @halley Thank you for giving me an example. This made it loud and clear to me.
     
    r3Fuze likes this.
  27. Offline

    halley

    This is the key, it is very important to remember. Each data structure has its strengths, and its weaknesses. That's why there are so many of them.
     
  28. Offline

    Sagacious_Zed Bukkit Docs

    @halley @bergerkiller
    You should copy and paste that into the wiki so it does not get lost int he forums. It is very good analogy at explaining the different use cases of the different collections.

    If you want ill handle the formatting.
     
    halley likes this.
  29. @halley is my hero for a long time :)
     
    tips48 and ZNickq like this.
Thread Status:
Not open for further replies.

Share This Page