user2444474
user2444474

Reputation: 573

TreeMultimap iterate issue

I'm not able to iterate the values from treemultimap. Please tell me what wrong here...i want to print the values as natural ordering as

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(",", " ")); 
                   } 

Upvotes: 1

Views: 1156

Answers (1)

jlordo
jlordo

Reputation: 37823

Don't over-complicate, it can be done in 3 lines:

    for (String key : mp.keySet()) {
        System.out.println(key + ": " + mp.get(key));
    }

Upvotes: 3

Related Questions