地图地图 - 如何将内部地图保存为地图? [英] Map of maps - how to keep the inner maps as maps?

查看:176
本文介绍了地图地图 - 如何将内部地图保存为地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建地图的地图,以便我可以通过其关键字检索外部地图的信息,然后通过它们的键访问其内部地图。然而,当我获得每个内部地图时,我创建的地图最初成为一个对象,并且我不能像使用外部地图一样使用键来访问它的值。 / p>

为了向您学习专家,我想知道如何将所有地图保存为地图。或者,有没有可能?



这是我的锻炼计划:

  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>();

//通过put方法键和值对在Map中添加或设置元素
mp.put(new Integer(2),Two);
mp.put(new Integer(1),One);
mp.put(new Integer(3),Three);
mp.put(新整数(4),四);

地图< 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(新的整数(4),Four2);

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

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

System.out.println(这是地图的地图:+ 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));
}
}
}

解决方案:

 映射< Object,Map< Object,String> 
地图< String,Object> mpMaps = new HashMap< String,Object>();

by ameer and sleske

解决方案

以下是更新后的代码,您需要将地图映射为< String,Object> ,因为mp isn'你不能做的字符串< 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>();

//通过put方法键和值对在Map中添加或设置元素
mp.put(new Integer(2),Two);
mp.put(new Integer(1),One);
mp.put(new Integer(3),Three);
mp.put(新整数(4),四);

地图< 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(新的整数(4),Four2);

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

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

System.out.println(这是地图的地图:+ 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));
}
}
}


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

解决方案

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));
        }             
    }
}

这篇关于地图地图 - 如何将内部地图保存为地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆