我无法迭代treemultimap中的值。请告诉我这里有什么问题...我想将值打印为自然排序
a 11 12
a 11 12
b 13 1 2 3
b 13 1 2 3
b 13 1 2 3
b 13 1 2 3
c 14
public static void main(String[] args) {
TreeMultimap<String, Integer> mp = TreeMultimap.create();
mp.put("a", 10);
mp.put("a", 11);
mp.put("a", 12);
mp.put("b", 13);
mp.put("c", 14);
mp.put("e", 15);
mp.put("b", 1);
mp.put("b", 2);
mp.put("b", 3);
List list = null;
Iterator i = mp.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
list=(List)mp.get(me.getKey());
for(int j=0;j<list.size();j++)
{
System.out.println(me.getKey()+": value :"+list.get(j).toString() .replaceAll("^\\[|\\]$", "").replaceAll(",", " "));
}
答案 0 :(得分:3)
不要过于复杂,可以用3行完成:
for (String key : mp.keySet()) {
System.out.println(key + ": " + mp.get(key));
}