HashMaps - Reading and Making

Discussion in 'Resources' started by Jaker232, Dec 13, 2011.

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

    Jaker232

    Forget this tutorial too many complaints about it sucking.
     
  2. Offline

    Sleaker

    #1 - generally you don't use uppercase names for Variables, unless they are constants, which you do all uppercase for them.
    #2 - generally if you'll be using boolean values you may as well use a Set - not a HashMap - as it generally equates to simpler looking code, and simpler checks overall.
    #3 - Your title has little to do with the actual content. You don't go over good ways to use hashmaps, or when one would be appropriate to implement and why.
    #4 Please stop posting public tutorials that teach people how to do things inefficiently or poorly. If you would still like to help people I suggest posting a question thread asking the other devs on the forum how they would approach a solution for doing something and give an example on how you might do it. Continually posting tutorials about things that you are just learning yourself may be helpful to some, but it also continues to produce poor quality plugins in the community.

    I would suggest something more like:

    Set<String> allowedPlayers = new HashSet<String>();

    if (allowedPlayers.contains(player.getName()) { // do some cool stuff }
     
    fysics and garbagemule like this.
  3. Offline

    feildmaster Retired Staff

    /me prefers lists because they iterate faster (and smaller size)... :D
     
    garbagemule likes this.
  4. Offline

    SwearWord

    @Jaker232
    While we do appreciate your eagerness to help, perhaps focus it in some other area where you have some experience. Much of what you posted here is not so good advice and a lot of it goes against convention. Your example is perfect for when you should not use a HashMap.

    If you wish to help as well as learn, perhaps idle in #bukkitdev irc channel in irc.esper.net where you can contribute on topics you know about as well as learn what you're doing wrong.
     
  5. Offline

    Jaker232

    Ahh forget my tutorials.
     
  6. Offline

    desht

    It's faster to iterate over a list, yes. But if you're regularly checking for membership (i.e. contains()) or retrieving items by a non-numeric key, then a HashSet/HashMap is much faster, unless you know for certain that your collection will never grow beyond a trivial number of elements.
     
  7. Offline

    Kaikz

    Wouldn't using an ArrayList for this kind of thing be faster?

    OP seems slightly mad.
     
  8. Offline

    feildmaster Retired Staff

    lol. You should have been in the IRC convo.
     
  9. Offline

    desht

    Clearly I missed something then :)
     
  10. Offline

    Sleaker

    No. .contains on a hashset with strings is generally going to be O(1) - that means the first item it goes to check for equality will be the object it's looking for (or wont exist), and then it will return true due to hashing or false if the hash doesn't exist.

    if you .contains on an ArrayList you're basically doing a for loop over the entire contents of the array. The only time it would be faster is if there was only 1 object in the list to check.
     
Thread Status:
Not open for further replies.

Share This Page