Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map of Maps data structure

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.

like image 942
Dónal Avatar asked Jun 22 '10 13:06

Dónal


People also ask

Which data structure is used in maps?

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.

What is the structure of map?

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.

What is data structure used in HashMap?

It uses an array and LinkedList data structure internally for storing Key and Value. There are four fields in HashMap.

Which data structure is used by map tree?

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.


2 Answers

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")
like image 155
Roman Avatar answered Sep 19 '22 17:09

Roman


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);
like image 45
Chris Dutrow Avatar answered Sep 22 '22 17:09

Chris Dutrow