0
votes

I have the following code:

/*
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
}

The above code works well if i want to iterate through Map. But now i have something like this.

/*
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??
}

I want to iterate through the Map within the Map and get the key and values for it. I want to get the value of key1,value1,key2,value2,key3,value3, and so on. So how should i go about it?

4
Why don't you use a List instead of a Map? If keys go from 1 to N, you could simply use an index (which would go from 0 to N - 1)JB Nizet

4 Answers

1
votes

You can use:

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

First get will get the correct Map<String, String> (for keys 1 or 2).

Second get will get the correct value (for key1, key2 ...)

0
votes

You might be better looking at something like Guava's 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

But without using that:

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);
}};
}
0
votes
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
        }
    }
}
-1
votes

Try this

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