hashmap vs. map

Discussion in 'Plugin Development' started by dralletje, Oct 17, 2011.

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

    dralletje

    first, I was addicted to arrays. I loved them.
    then I saw arrayLists, it was great to edit the size of it without much commands and such.
    but then I saw Maps. now I love them!

    but I see on this forum, I never see maps. only hashmaps.
    So my question is, is a map the best way to store things like <Object key, Object value>? and what is an hashmap?
     
  2. Offline

    desht

    Map is an interface. HashMap is an implementation of Map (or more precisely, a subclass of AbstractMap, which is an implementation of Map) which uses a hashing algorithm to map keys to values. It's probably the most commonly-used implementation of maps, but it isn't the only one (e.g. there's also a TreeMap which uses a red-black tree to maintain a sort order).

    http://download.oracle.com/javase/6/docs/api/java/util/Map.html
    http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html

    A hashmap is probably what you want to use here. This is valid code:
    Code:java
    1.  
    2. Map<String,Object> myMap = new HashMap<String,Object>();
    3.  


    In general, it's fine just to create a variable of the interface type (Map), assigning it to an actual HashMap object. You might want to assign to an actual HashMap if, for example, you needed to clone() it (HashMap implements Clonable).
     
  3. Offline

    dralletje

    ok thanks :)
    I thought I should make hashes of thing or anything like that :S
    but it is much simpler.
     
  4. Offline

    bergerkiller

    Maybe off-topic...but I got this HUGE line of code and wonder if it can be done differently.
    I basically have to make a 4-dimensional hashmap, storing world, x, y and z:
    Code:
    private static HashMap<World, HashMap<Integer, HashMap<Integer, HashMap<Integer, Redstone>>>> redstone = new HashMap<World, HashMap<Integer, HashMap<Integer, HashMap<Integer, Redstone>>>>();
    Reason I don't use a 'position' class is that the HashCode easily fails the more items you have in there.
    Any ideas?

    Would a long list of elements beat the hashmap in this case?
     
  5. Offline

    Afforess

    Gross. What Spout does is keep 1 hashmap for each chunk and then you can fit the entire x,y,z coord into an integer. So the coords become a key. We made a wrapper class to make a 3 key int map easy to use, which you are free to use: http://github.com/SpoutDev/SpoutAPI.../spoutapi/util/map/TByteTripleIntHashMap.java

    Also, we use Trove primitive collections wherever possible because they use ~30% less memory and have about 600% faster lookups that java hashmaps. The example I posted was for an integer key, but switching to an object key is trivial.
     
Thread Status:
Not open for further replies.

Share This Page