【发布时间】:2011-07-16 10:17:23
【问题描述】:
我想从HashMap 中检索k,v-pairs。
条目是这样的:
a = 3,4
b = 5,6
等等。我需要这些值的组合。
a=3, b=5
a=3, b=6
a=4, b=5
a=4, b=6
我不知道值有多少个键和多少个条目。使用entrySet 我可以获得值但不能获得组合。它看起来像递归,但如何?
这是我的代码:
HashMap<String, String[]> map = new HashMap<String, String[]>();
BufferedReader file = new BufferedReader(new FileReader("test.txt"));
String str;
while ((str = file.readLine()) != null) {
// ... logic
map.put(key, value);
}
System.out.println("number of keys: " + map.size());
for (Map.Entry<String, String[]> entry : map.entrySet()) {
for (String value : entry.getValue()) {
System.out.println(entry.getKey() + ": " + value);
}
}
file.close();
【问题讨论】:
-
我不清楚:“a”是您的键,“3”(来自 a 的列表)是您的值,还是“3”是 a 的键和“5”是 b 的值?
-
a 是键,3 和 4 是字符串。因此“字符串 []”。值为 String[]
标签: java recursion hashmap combinations cartesian-product