0
我应该如何通过这种声明迭代嵌套地图?如何遍历嵌套地图和Multiset? - Java/Guava
Map<String, Multiset<String>>
请建议,如果有其他的HashMap /列表是更有效的做这个哈希人口任务的方式吗?
import com.google.common.collect.Multiset;
import com.google.common.collect.TreeMultiset;
String[] foobarness = {"foo" , "bar", "ness", "foo",
"bar", "foo", "ness", "bar", "foo", "ness", "foo",
"bar", "foo", "ness", "bar", "ness", "foo", "bar",
"foo", "ness"};
String[] types = {"type::1", "type::2", "type::3",
"type::4",};
Map<String, Multiset<String>> typeTextCount =
new HashMap<String, Multiset<String>>();
Multiset<String> textAndCount
= TreeMultiset.create();
for (int i=0; i<types.length; i++) {
// I know it's kinda weird but in my task,
// i want to keep adding only 1 to the count for each entry.
// Please suggest if there is a better hashmap/list for such task.
if ((types[i]== "type::1") or (types[i]== "type::3")) {
for (String text : foobarness) {
// I don't worry too much about how i
// populate the Map, it is iterating through
// the Map that I have problem with.
textAndCount.put(text, 1);
}
}
if ((types[i]== "type::2") or (types[i]== "type::4")) {
for (String text : foobarness)
textAndCount.put(text, 1);
}
}
所以现在hashmap填充,我如何遍历复杂的嵌套地图? 我试着下面的代码,但我只从我的多集了1号的getValue():
Iterator<Entry<String, Multiset<String>>> itTTC =
typeTextCount.entrySet().iterator();
while (itTTC.hasNext()) {
Map.Entry textCt = (Map.Entry)itTTC.next();
System.out.println(textCt.getKey() + " :\t" + textCt.getValue());
itTTC.remove();
}
你在哪里把你的地图元素? – soulcheck 2011-12-15 15:27:54
顺便说一下,你甚至尝试调试器? – Scorpion 2011-12-15 15:32:56