Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what java collection that provides multiple values for the same key

what type of java collection that returns multiple values for the same key?

example, I want to return 301,302,303 for key 300.

like image 448
palAlaa Avatar asked Jul 27 '11 20:07

palAlaa


People also ask

Can we have multiple values for same key in Java?

Java collection classes allow a developer to keep track of contained items in one of two ways, either by an indexed count or a key. This creates an obvious limitation where multiple values can't be associated with the same key or index.

Can a Map have multiple values for same key Java?

Java has several implementations of the interface Map, each one with its own particularities. However, none of the existing Java core Map implementations allow a Map to handle multiple values for a single key.

Can a single key have multiple values?

Each key can only have one value. But the same value can occur more than once inside a Hash, while each key can occur only once.


3 Answers

You can use a List as the value of your Map:

List<Integer> list = new ArrayList<Integer>();
list.add(301);
list.add(302);
list.add(303);

Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
map.put(300, list);

map.get(300); // [301,302,303]

Alternatively, you can use Multimap from Guava, as suggested by biziclop, which has a much cleaner syntax, and lots of other very useful utility methods:

Multimap<Integer, Integer> map = HashMultimap.create();
map.put(300, 301);
map.put(300, 302);
map.put(300, 303);

Collection<Integer> list = map.get(300); // [301, 302, 303]
like image 120
João Silva Avatar answered Oct 21 '22 04:10

João Silva


You could use Multimap, it is under the Apache license.

See this link. For posterity:

org.apache.commons.collections
Interface MultiMap

All Superinterfaces:
    java.util.Map

All Known Implementing Classes:
    MultiHashMap, MultiValueMap

public interface MultiMap
extends java.util.Map

Defines a map that holds a collection of values against each key.

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". 
like image 21
Bernd Elkemann Avatar answered Oct 21 '22 05:10

Bernd Elkemann


  1. As mentioned in a comment above above there is always Guava Multimap
    http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html

  2. Apache Commons Collections 4 has Generic version of MultiMap http://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiMap.html

  3. JAX-RS specifies a MultivaluedMap interface which is implemented by all JAX-RS Providers. If your use case is within the context of a JAX-RS REST Service/Client, it would be an option to use their implementation without pulling in another dependency.

    javax.ws.rs.core.MultivaluedMap (each JAX RS Provider has their own implementation)

like image 39
Tarun Avatar answered Oct 21 '22 05:10

Tarun