我有以下代码:
/*
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等的值 . 那我该怎么办呢?
4 回答
您可以使用:
首先得到正确的
Map<String, String>
(对于键1或2) .第二次获取将获得正确的值(对于key1,key2 ......)
你可能会更好地看看像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
但没有使用它:
试试这个