Map of maps - how to keep the inner maps as maps?

109,534

Solution 1

Here is the updated code that seems to work, you need to type the map of maps as <String, Object> since mp isn't a string you can't do <Object, String>.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;

public class MapExample {

    public static void main(String[] args) {

        Map<Object,String> mp=new HashMap<Object, String>();

        // adding or set elements in Map by put method key and value pair
        mp.put(new Integer(2), "Two");
        mp.put(new Integer(1), "One");
        mp.put(new Integer(3), "Three");
        mp.put(new Integer(4), "Four");

        Map<Object,String> mp2=new HashMap<Object, String>();
        mp2.put(new Integer(2), "Two2");
        mp2.put(new Integer(1), "One2");
        mp2.put(new Integer(3), "Three2");
        mp2.put(new Integer(4), "Four2");

        Map<String, Object> mpMaps=new HashMap<String, Object>();

        mpMaps.put("Map1",mp);
        mpMaps.put("Map2",mp2);

        System.out.println("This is a map of Maps:   " + mpMaps); 

        for (int i=0;i<mpMaps.size();i++){
                     ArrayList<Object> a = new ArrayList<Object>(mpMaps.keySet());
                     Object o=a.get(i);
                     System.out.println("all together: " + mpMaps.size() + "each element is:  " + o + " value: " + mpMaps.get(o));
        }             
    }
}

Solution 2

An alternate solution would be to use Commons MultiKey for avoiding map of maps. See details at http://commons.apache.org/collections/apidocs/ and org.apache.commons.collections.keyvalue.MultiKey

Solution 3

Your code does not compile.

One problem is this:

Map<Object,String> mpMaps=new HashMap();
mpMaps.put("Map1",mp);

This won't work, as you put a Map (mp) into a Map whose values need to be Strings.

Use Map<Object,Map<Object,String> and you should be fine.

Solution 4

Here is a simple example using HashMap

    HashMap<String, HashMap<String, Object>> outerMap = new HashMap<String, HashMap<String, Object>>();

    HashMap<String, Object> innerMap1 = new HashMap<String, Object>();
    HashMap<String, Object> innerMap2 = new HashMap<String, Object>();


    innerMap1.put("Name", "az");
    innerMap1.put("Address", "7 lab");
    innerMap1.put("Age", 40);

    innerMap2.put("Name", "sab");
    innerMap2.put("Address", "bk3");
    innerMap2.put("Age", 35);

    outerMap.put("1",innerMap1);
    outerMap.put("2", innerMap2);
    System.out.println("outerMap = " + outerMap);

output:

outerMap = {1={Address=7 lab, Age=40, Name=az}, 2={Address=bk3, Age=35, Name=sab}}
Share:
109,534
john
Author by

john

biostatisticians for 17 years; manager, director, president of various CROs. SAS expert for purpose of FDA submission.

Updated on July 05, 2020

Comments

  • john
    john almost 4 years

    My goal is to create a map of maps so that I can retrieve info of the outer map by its key and then access its "inner" maps by their keys.

    However, when I got each inner map, the map I created originally became an Object and I cannot use key to access its value as I do with the outer map.

    To learn from you experts, I would like to know how to keep all the maps as maps. Or, is it possible at all?

    here is my exercise program:

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    public class MapExample {
    
        public static void main(String[] args) {
    
            Map<Object,String> mp=new HashMap<Object, String>();
    
            // adding or set elements in Map by put method key and value pair
            mp.put(new Integer(2), "Two");
            mp.put(new Integer(1), "One");
            mp.put(new Integer(3), "Three");
            mp.put(new Integer(4), "Four");
    
            Map<Object,String> mp2=new HashMap<Object, String>();
            mp2.put(new Integer(2), "Two2");
            mp2.put(new Integer(1), "One2");
            mp2.put(new Integer(3), "Three2");
            mp2.put(new Integer(4), "Four2");
    
            Map<Object,String> mpMaps=new HashMap();
    
            mpMaps.put("Map1",mp);
            mpMaps.put("Map2",mp2);
    
            System.out.println("This is a map of Maps:   " + mpMaps); 
    
            for (int i=0;i<mpMaps.size();i++){
                         ArrayList a = new ArrayList(mpMaps.keySet());
                         Object o=a.get(i);
                         System.out.println("all together: " + mpMaps.size() + "each element is:  " + o + " value: " + mpMaps.get(o));
            }             
        }
    }
    

    SOLUTIONS:

       Map<Object,Map<Object,String>
        Map<String, Object> mpMaps=new HashMap<String, Object>(); 
    

    by ameer and sleske

  • john
    john over 13 years
    by redefining the map, should i get a map by Map m = mpMaps.get(key)? I tried, but still can not get it to work.....
  • sleske
    sleske over 13 years
    Yes, you should. If it does not work, edit your question to add the new code and explain exactly what does not work.
  • john
    john over 13 years
    Excellent! I got it working! Now, really understand how serious Java is! A few times, I got code working in Groovy, but when tried on Java, it failed. It failed because I did not give enough detail of the types! THANK YOU!