3

Is it possible to have multiple values, for a single key, in the Java dictionary class?

Adrian Toman
  • 11,316
  • 5
  • 48
  • 62
  • See my answer below regarding Dictionary - it is considered an obsolete class. – Uri Jun 27 '09 at 05:45
  • What Java Dictionary class? Do you mean Hashtable? – oxbow_lakes Jun 27 '09 at 08:35
  • This looks like a replay of this question: http://stackoverflow.com/questions/1049833/multi-valued-hashtable-in-java, posted some 12 hours earlier. Both were asked by someone named Raji, but using different unregistered accounts. – Jonik Jun 27 '09 at 08:55
  • @oxbow, http://java.sun.com/javase/6/docs/api/java/util/Dictionary.html. I didn't know about this either, but Dictionary is an abstract superclass of Hashtable - and it's obsolete as Uri said. This question is quite clearly a duplicate. – Jonik Jun 27 '09 at 09:05

4 Answers4

3

First, regarding the dictionary class: That class is considered obselete, the documentation recommends using Map instead.

This kind of collection you are seeking is called a multimap. You could implement one yourself with a list, but that is tedious.

You may want to use a MultiMap from either Apache Collections or from the Google Collections. While I am personally a fan of the apache collections, they do not really support generics, so a Google multimap may be safer.

Uri
  • 88,451
  • 51
  • 221
  • 321
2

Sure. Use a list as the value.

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
2

You should take a look at the MultiMap class from the apache commons collections

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

You can use a regular Map and have the values be Collections:

Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();

map.put(0, Arrays.asList("foo", "bar"));
map.put(1, new ArrayList<String>());

map.get(1).add("blag");

Or you can use MultiMap from the Apache Commons Collections.

A MultiMap is a Map with slightly different semantics. Putting a value into the map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.

For example:

MultiMap mhm = new MultiHashMap();
mhm.put(key, "A");
mhm.put(key, "B");
mhm.put(key, "C");
Collection coll = (Collection) mhm.get(key);

coll will be a collection containing "A", "B", "C".

(Unfortunately, MultiMap does not use generics.)

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • you can't use the type "int" as a generic parameter, because it's a primitive type. use Integer instead. – cd1 Jun 27 '09 at 05:04
  • 1
    Google Collections would be much preferable to Apache Commons Collections. It's implemented with generics and other Java 5 features, and overall seems to be a better extension for Java Collections framework. See http://www.javalobby.org/articles/google-collections/ (search for "Apache Commons") – Jonik Jun 27 '09 at 09:00
  • True, If there is no constraint such as a Java 1.4 compatible solution IMHO. – akarnokd Jun 27 '09 at 09:31