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?
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 Map<String,Object> myMap = new HashMap<String,Object>(); 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).
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?
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.