繁体   English   中英

地图地图 - 如何将内部地图保留为地图?

[英]Map of maps - how to keep the inner maps as maps?

我的目标是创建一个地图地图,以便我可以通过其键检索外部地图的信息,然后通过其键访问其“内部”地图。

但是,当我得到每个内部地图时,我创建的地图最初变成了一个对象,我不能像使用外部地图那样使用键来访问它的值。

要向您学习专家,我想知道如何将所有地图保留为地图。 或者,它有可能吗?

这是我的锻炼计划:

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

解决方案:

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

由ameer和sleske

以下是似乎有效的更新代码,您需要将地图的地图键入为<String, Object>因为mp不是您不能执行的字符串<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));
        }             
    }
}

您的代码无法编译。

一个问题是:

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

这不起作用,因为您将Map( mp )放入其值必须为Strings的Map中。

使用Map<Object,Map<Object,String> ,你应该没问题。

另一种解决方案是使用Commons MultiKey来避免地图的映射。 请参阅http://commons.apache.org/collections/apidocs/和org.apache.commons.collections.keyvalue.MultiKey上的详细信息

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

mpMaps.put("Map1",mp);

你会得到这个语句的异常:mp有类型Map但你把它当作一个String。

如果我理解你的问题,你需要引用用作键的对象,或者你需要键入转换你的键/值。

这是一个使用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);

输出:

outerMap = {1={Address=7 lab, Age=40, Name=az}, 2={Address=bk3, Age=35, Name=sab}}

例

这是创建地图地图的示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM