【问题标题】:How to iterate through a map with a map in Java如何用Java中的地图遍历地图
【发布时间】:2013-12-15 08:36:31
【问题描述】:

我有以下代码:

/*
if inputDetailsMap has values like:
String      String
 key1       value1
 key2       value2
 key3       value3
 key4       value4
 key5       value5
so on.....
*/

public void inputData(Map<String,String> inputDetailsMap, String fileName){
    //a for loop to run
          String value = inputDetailsMap.get("key" + i);    // i value comes from the for loop
          //i am doing something with the value.
    //end of for loop
}

如果我想遍历 Map,上面的代码效果很好。 但现在我有这样的东西。

/*
inputDetailsMap has values like:
int         String      String
 1           key1       value1
             key2       value2
             key3       value3
             key4       value4
             key5       value5
 2           key1       value1
             key2       value2
             key3       value3
             key4       value4
             key5       value5
so on.....
*/
 public void inputData(Map<Integer,Map<String,String>> inputDetailsMap, String fileName){
      //how to iterate and get the value using inputDetailsMap.get() like the above code??
}

我想遍历 Map 中的 Map 并获取它的键和值。 我想获取key1、value1、key2、value2、key3、value3等的值。 那我该怎么办呢?

【问题讨论】:

  • 为什么不使用列表而不是地图?如果键从 1 到 N,您可以简单地使用索引(从 0 到 N - 1)

标签: java map


【解决方案1】:

你可以使用:

String value = inputDetailsMap.get(1).get("key" + i);

第一次获取将获得正确的Map&lt;String, String&gt;(对于键 1 或 2)。

第二次get会得到正确的值(对于key1,key2 ...)

【讨论】:

    【解决方案2】:

    你最好看看 Guava 的 Multimap 之类的东西:

    http://tomjefferys.blogspot.co.uk/2011/09/multimaps-google-guava.html

    http://guava-libraries.googlecode.com/svn-history/r14/trunk/javadoc/com/google/common/collect/Multimap.html

    但不使用它:

    import java.util.HashMap;
    import java.util.Map;
    
     public class MapInMap {
    
    public static void main(String[] args){
    
        for (Integer index: outerMap.keySet()){
            for (String innerIndex: outerMap.get(index).keySet()){
                System.out.println(String.format("%s :: %s :: %s", index, innerIndex, outerMap.get(index).get(innerIndex)));
            }
        }
    
    }
    
    /*
    inputDetailsMap has values like:
    int         String      String
     1           key1       value1
                 key2       value2
                 key3       value3
                 key4       value4
                 key5       value5
     2           key1       value1
                 key2       value2
                 key3       value3
                 key4       value4
                key5       value5
    so on.....
    */
    static Map<String, String> innerMap1 = new HashMap<String, String>(){{
        put("key1", "value1");
        put("key2", "value2");
        put("key3", "value3");
        put("key4", "value4");
        put("key5", "value5");
    }};
    static Map<String, String> innerMap2 = new HashMap<String, String>(){{
        put("key1", "value1");
        put("key2", "value2");
        put("key3", "value3");
        put("key4", "value4");
        put("key5", "value5");
    }};
    
    static Map<Integer, Map<String,String>> outerMap = new HashMap<Integer, Map<String,String>>(){{
        put(1, innerMap1);
        put(2, innerMap2);
    }};
    }
    

    【讨论】:

      【解决方案3】:
      public void inputData(Map<Integer,Map<String,String>> inputDetailsMap) {
          for (Map.Entry<Integer, Map<String,String> entry : inputDetailsMap.entrySet()) {            
              Map<String, String>innerMap = entry.getValue()
              for (Map.Entry<String, String> keyPair : innerMap.entrySet()) {
                  String key = keyPair.getKey();
                  String value = keyPair.getValue();
                  // do something with value
              }
          }
      }
      

      【讨论】:

      • 不会编译为类型参数不能是原始类型。
      • 将 int 更改为 Integer
      【解决方案4】:

      试试这个

      public void inputData(Map<int,Map<String,String>> inputDetailsMap, String fileName){
          System.out.println(inputDetailsMap.get(fileName));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-18
        • 2011-03-23
        • 2014-11-09
        相关资源
        最近更新 更多