Skip to main content

APIzation: Replication Package

[Q#4120216][A#4120268] Map of maps - how to keep the inner maps as maps?

https://stackoverflow.com/q/4120216

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: SOLUTIONS: by ameer and sleske

Answer

https://stackoverflow.com/a/4120268

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>.

APIzation

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));
        }             
    }
}
package com.stackoverflow.api;

import java.util.HashMap;
import java.util.Map;

public class Human4120268 {

  public static Map<String, Object> createMapOfMaps() {
    Map<Object, String> mp = new HashMap<>();

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

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

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

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

    return mpMaps;
  }
}

package com.stackoverflow.api;

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

/**
 * Map of maps - how to keep the inner maps as maps?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/4120268">https://stackoverflow.com/a/4120268</a>
 */
public class APIzator4120268 {

  public static Map<String, Object> map(
    Map<Object, String> mp,
    Map<Object, String> mp2,
    Map<String, Object> 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)
      );
    }
    return mpMaps;
  }
}