Itetrate Map <string,map <string,=“”string =“”>&gt;使用java

时间:2015-06-11 13:23:47

标签: java

我有一个用于存储平台详细信息的hashmap 我需要迭代Map<String, Map<String, String>> finalmapWin8 并获取mapWin8的值和键 values 33,8和密钥BrowserType.CHROME, BrowserType.FIREFOX and BrowserType.IE 我也希望获得'finalmapWin8' "WIN8_1"的密钥 我如何迭代Map<String, Map<String, String>> finalmapWin8

 static Map<String, Map<String, String>> finalmapWin8 = new HashMap<Platform, Map<String, String>>();

    public static final Map<String, String> mapWin8 = new HashMap<String, String>();
    static {
        mapWin8.put(BrowserType.CHROME, "33");
        mapWin8.put(BrowserType.FIREFOX, "33");
        mapWin8.put(BrowserType.IE, "8");
    }   
    static {
        finalmapWin8.put("WIN8_1", mapWin8);      
    }

4 个答案:

答案 0 :(得分:2)

Map的迭代是相同的。因为你的地图的价值也是一个地图,那么你只需要另一次迭代来迭代它。

    for(String s: finalmapWin8.keySet()){
        System.out.println(s + " : ");
        for(Entry<String, String> entry : finalmapWin8.get(s).entrySet()){
           System.out.println(entry);
        }
    }

答案 1 :(得分:1)

你可以为Map.Entry<String, Map<String, String>>类型的Map使用for循环for循环......好吧,就是这样..请参阅下面的代码片段 -

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

public class Test {

    static Map<String, Map<String, String>> finalmapWin8 = new HashMap<String, Map<String,String>>();
    public static final Map<String, String> mapWin8 = new HashMap<String, String>();

    static {
        mapWin8.put("CHROME", "33");
        mapWin8.put("FIREFOX", "33");
        mapWin8.put("IE", "8");
    }   
    static {
        finalmapWin8.put("WIN8_1", mapWin8);      
    }

    public static void main(String[] args) {


                for(Map.Entry<String, Map<String, String>> entry : finalmapWin8.entrySet()) {
                    System.out.println(entry.getValue());
                }


    }
}

答案 2 :(得分:0)

//获取键集或外部地图,然后再为内部地图运行一个secound循环。

for (Map.Entry<String, Map<String, String>> entry : firstMap) {
  system.out.println("entry="+entry );
}

请参阅How to efficiently iterate over each Entry in a Map?

答案 3 :(得分:0)

您可以使用以下代码迭代HashMap

for (Map.Entry<String, Map<String, String>> entry : finalmapWin8.entrySet()) {

           System.out.println("Key:"+entry.getKey());

           for (Map.Entry<String, String> entry1 : mapWin8.entrySet()) {
              System.out.println("Keys in Second:"+ entry1.getKey()+" Values:"+ entry1.getValue());
           }
        }