Friday, January 16, 2009

Multi Value Map : The values are a List

Be aware that the method put does more than just "put" an object into the map. Therefore, if this object is the by-product of a method it would be advisable to wrap the map in an un-modifiable map. A lot of developers argue that this object violates the map interface. Frankly I think this argument is akin to splitting hairs, it works for me and I use it everyday.

The code:

MultiMap mhm = new MultiValueMap();

key = "Group One";
mhm.put(key, "Item One");
mhm.put(key, "Item Two");
mhm.put(key, "Item Three");

key = "Group Two";
mhm.put(key, "Item Four");
mhm.put(key, "Item Five");

Set keys = mhm.keySet();
for (Object k : keys) {
out.println("("+k+“ : "+mhm.get(k)+")");

The results:

(Group One : [Item One, Item Two, Item Three])
(Group Two : [Item Four, Item Five])
Author: Philip A Senger


  1. I got this web page from my friend who shared with me on the topic of
    this website and now this time I am browsing this web
    page and reading very informative content at this time.

    My webpage - site referencement google ()

  2. i have dont this but when i copied it to the list after the for loop here its returning the value in reverse order

    (Group Two : [item Four, item Five])
    item four
    item five
    (Group One : [item One, item Two, item Three])
    item one
    item two
    item three

    how can i solve this