The MultiValueMap class (Apache commons collections) makes it easy to work with a Map whose values are Collections. I'm looking for a a class that makes it easy to work with a Map whose keys are objects and values are Maps.
I'm using Java 1.4, so can't use Google Collections or generics.
Now coming to the point that which data structure is used by map. The internal implementation of map is self-balancing Binary Tree. There are generally two methods for implementing a self-balancing binary tree: Red-Black Tree and.
A type of subsurface map whose contours represent the elevation of a particular formation, reservoir or geologic marker in space, such that folds, faults and other geologic structures are clearly displayed.
It uses an array and LinkedList data structure internally for storing Key and Value. There are four fields in HashMap.
In TreeMap, the values are ordered by the key. The HashTable does not allow null keys or values and is synchronized. The map data type is known as an associative array because, like an array, it is a collection of values and not a single value like an Int or a String.
Map of maps is actually a tree-type structure without single root node (as well as map of maps of maps...).
You can look at Composite pattern which is widely used for implementing tree structures (if their components has the same type which is not the case as I feel).
Another solution is to implement a simple domain model. It'll be much clearer to read and easy to maintain something like:
school.getPupil ("John Doe").getMark ("Math")
than
school.get ("John Doe").get ("Math")
The regular Map collection works for this:
Map<Object,Map<Object,Object>> mapOfMaps = new LinkedHashMap<Object,Map<Object,Object>>();
Object newObject = new String("object as string");
mapOfMaps.put(newObject, new LinkedHashMap<Object,Object>());
Map<Object,Object> objectMap = mapOfMaps.get(newObject);
In fact, if you're not worried about type safety, you can put whatever you want into the value section:
Map<Object,Object> mapOfWhatever = new LinkedHashMap<Object,Object>();
Object newObject = new String("object as string");
mapOfWhatever.put(newObject, new LinkedHashMap<Object,Object>());
Map<Object,Object> objectMap = (Map<Object, Object>) mapOfWhatever.get(newObject);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With