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

发布于 2024-10-01 18:28:17 字数 1633 浏览 1 评论 0 原文

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

但是,当我获得每个内部映射时,我创建的映射最初变成了一个对象,并且我无法像使用外部映射一样使用键来访问它的值。

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

这是我的锻炼计划:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

比忠 2024-10-08 18:28:17

这是似乎有效的更新后的代码,您需要将地图的地图键入 因为 mp 不是字符串,您不能这样做

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

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));
        }             
    }
}
肥爪爪 2024-10-08 18:28:17

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

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

不…忘初心 2024-10-08 18:28:17

您的代码无法编译。

一个问题是:

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

这不起作用,因为您将一个 Map (mp) 放入一个其值需要为 Strings 的 Map 中。

使用 Map 应该没问题。

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.

南烟 2024-10-08 18:28:17

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

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}}
妄司 2024-10-08 18:28:17
Map<Object,String> mpMaps=new HashMap();

mpMaps.put("Map1",mp);

您将得到以下语句的异常: mp 具有 Map 类型,但您将其视为字符串。

如果我正确理解你的问题,你要么需要对用作键的对象的引用,要么需要类型转换你的键/值。

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

mpMaps.put("Map1",mp);

you'll get an exception with this statement: mp has type Map but you're treating it as a String.

If I understood your question right, you'll either need a reference to the object used as key or you'll need to type cast your key/values.

温柔戏命师 2024-10-08 18:28:17

示例

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

Example

This is an example on creating a map of maps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文