Create Multidimensional HashMap in java

Pablo picture Pablo · Feb 27, 2014 · Viewed 7.7k times · Source

I am kind of new to java, I have this code

...
NodeList indexEntryList = sourceDoc.getElementsByTagName("in.ar");  //this is a xml tag
for (int i = 0; i < indexEntryList.getLength(); i++) {
    ...
}
...

I have to create a HashMap (or something like this), where I save a String which is an attribute of the node, and a List of all nodes that have the same attribute.

I think something like this:

Map<String, ArrayList<Node>> indexCategories = new HashMap<String, ArrayList<Node>>();

But in each time of the for, I dont know how to search in all keys of the Map, and add the new Node to the list, and if the key does no exists yet, create the new item inside the Map.

Answer

Smutje picture Smutje · Feb 27, 2014

Use Map#containsKey() for searching if a key is present, Map#get() for obtaining the collection (if present) respective Map#put() for storing the newly created map. Everything could be found properly documented in the Map API, by the way.