1

我无法从 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(",", " ")); 
                   } 
4

1 回答 1

3

不要过于复杂,它可以在 3 行中完成:

    for (String key : mp.keySet()) {
        System.out.println(key + ": " + mp.get(key));
    }
于 2013-06-03T00:19:22.677 回答