1 Reply Latest reply: Mar 22, 2012 8:22 AM by gimbal2 RSS

    Allow duplicate key & value in collection

    843998
      Hello Experts,

      I have once scenario where I have to store duplicate keys (string) and values (string) in Map. I tried with MultiHashMap (Apache) but it's very specific to datatype. It will only allow "string" key and "integer" value.

      How can I do it? Any help will be appreciated.

      Regards,
      Somnath
        • 1. Re: Allow duplicate key & value in collection
          gimbal2
          Stupid question perhaps, but a regular HashMap with a String as a key and an ArrayList as a value doesn't cut it? You really NEED duplicate keys also?

          EDIT: yes that is a stupid suggestion; of course you need duplicate keys when it is in fact a duplicate key/value pair that we're talking about.

          It will only allow "string" key and "integer" value.
          When I look at the Javadoc I see it is controlled by generics, just like the regular Map type. Here, even in this very old javadoc:

          http://commons.apache.org/collections/api-2.1.1/org/apache/commons/collections/MultiHashMap.html

          You can see that put() takes two Object values, not a String and an Integer.