简体   繁体   English

从地图获取地图的值

[英]Get values of Map from a Map

In my code i have a 在我的代码中,我有一个

Map<String,Map<String,customObject>>

I am not sure how to iterate over this map and get the values from it. 我不确定如何遍历此地图并从中获取值。 What i am trying to do here is get the enclosing Map by passing in the key to the external Map. 我在这里尝试通过将密钥传递给外部Map来获取封闭Map。 When i get the enclosing map i need to iterate over it and get key and value from it. 当我得到封闭的地图时,我需要对其进行迭代并从中获取键和值。 Can you please let me know how i can do this as i am kind of stuck here. 您能不能让我知道我该怎么做,因为我有点卡在这里。 Any example or code of a similar type can be of a great help to understand it better. 更好地理解任何类似类型的示例或代码都可以提供很大的帮助。

Thanks Vikeng21 谢谢Vikeng21

You can use the entry set of both Maps. 您可以使用两个地图的条目集。 something like this: 像这样的东西:

    Map<String,Map<String,String>> map1 = ...
    Set<Entry<String,Map<String,customObject>>> entrySet1 = map1.entrySet();
    for (Entry<String, Map<String, customObject>> entry1 : entrySet1) {
        Map<String,String> map2 = entry1.getValue();
        Set<Entry<String, customObject>> entrySet2 = map2.entrySet();
        for (Entry<String, customObject> entry2 : entrySet2) {
            System.out.println(entry1.getKey() +" -> "+entry2.getKey()+" -> "+entry2.getValue());
        }
    }

To iterate over hashmap entries... 要遍历hashmap条目...

for (Map.Entry<String, Map<String, Object>> ent : hashmap.entrySet())
{
    //ent.getKey();   is the key [String]
    //ent.getValue(); is the value [Map<String, Object>]
}

Now work out from there, it's basically the same. 现在从那里计算出来,基本上是一样的。

I am not sure how to iterate over this map and get the values from it 我不确定如何遍历此地图并从中获取值

You would iterate over the map's values like with any maps - see below an example that uses such a structure. 您将像使用任何地图一样迭代地图的值-参见下面使用这种结构的示例。

Map<String, CustomObject> innerMap = new HashMap<String, CustomObject> ();
innerMap.put("abc", new CustomObject());

Map<String, Map<String, CustomObject>> externalMap = new HashMap<String, Map<String, CustomObject>> ();

externalMap.put("map1", innerMap);

//iterate over all the maps contained in externalMap 
for (Map<String, CustomObject> inner : externalMap.values()) {
    System.out.println(inner);
}

If you also need to access the keys, you can iterate over the entry set: 如果还需要访问密钥,则可以遍历条目集:

for (Entry<String, Map<String, CustomObject>> e : externalMap.entrySet()) {
   System.out.println(e.getKey()); //map1
   System.out.println(e.getValue()); //innerMap
}

I think this example will give your answer.... 我想这个例子会给你答案。

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

public class MapInMap {
    public static void main(String[] args) {
        Map<String, MyObj> innerMap01 = new HashMap<String, MyObj>();
        Map<String, MyObj> innerMap02 = new HashMap<String, MyObj>();
        innerMap01.put("OneOne", new MyObj());
        innerMap02.put("TwoOne", new MyObj());

        Map<String, Map<String, MyObj>> maps = new HashMap<String, Map<String, MyObj>>();

        maps.put("One", innerMap01);
        maps.put("Two", innerMap02);

        for (Entry<String, Map<String, MyObj>> map : maps.entrySet()) {
            for (Entry<String, MyObj> innerObject : map.getValue().entrySet()) {
                // your logic
            }
        }
    }
}

class MyObj {
    int i;
}

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

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