Map
does not supports duplicate keys. you can use collection as value against same key.
Associates the specified value with the specified key in this map
(optional operation). If the map previously contained a mapping for
the key, the old value is replaced by the specified value.
Documentation
you can use any kind of List
or Set
implementation according to your requirement.
If your values might be also duplicate you can go with ArrayList
or LinkedList
, in case values are unique you can use HashSet
or TreeSet
etc.
Also In google guava collection library Multimap
is available, it is a collection that maps keys to values, similar to Map, but in which each key may be associated with multiple values. You can visualize the contents of a multimap either as a map from keys to nonempty collections of values:
a → 1, 2
b → 3
Example -
ListMultimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("a", "1");
multimap.put("a", "2");
multimap.put("c", "3");