7

I noticed that there is a MultiValueMap from commons, however it doesn't support generics. Is there such a map that does?

1
  • 1
    As aside, Java Commons' MultiValueMap cannot be retrofitted to support generics because of their contract for get(): "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." ( commons.apache.org/collections/apidocs/org/apache/commons/… ) Commented Oct 14, 2011 at 20:58

4 Answers 4

11

Have you tried Guava's Multimap?

A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.

Depending on the implementation, a multimap may or may not allow duplicate key-value pairs. In other words, the multimap contents after adding the same key and value twice varies between implementations. In multimaps allowing duplicates, the multimap will contain two mappings, and get will return a collection that includes the value twice. In multimaps not supporting duplicates, the multimap will contain a single mapping from the key to the value, and get will return a collection that includes the value once.

http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multimap.html

Sign up to request clarification or add additional context in comments.

Comments

5

Absolutely! Check out Google Guava's Multimaps.

Multimap<Foo, Bar> mm = new ListMultimap<Foo, Bar>();
// fill it however...
Foo foo = ...;
Collection<Bar> bars = mm.get(foo);

Comments

2

Probably Guava is a better choice but if you really want to stick with Commons collections API:

http://sourceforge.net/projects/collections

A Java 5 generics-enabled version of the popular Jakarta Commons-Collections project. All appropriate classes from Commons-Collections 3.1 have been refactored to support Java generics.

2 Comments

Looking the Javadocs for get(), I don't see how they are doing it right. They haven't changed the contract to stop returning Collection<V>, so it clashes with Map.get()'s return type...
OK I see now. Notice that they retain backward compatibility with the old contract by having MultiMap<K, V> extend the non-genericized Map. Or to put another way, Yuck.
0

https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 since Nov, 2013 supports generic collections! No need to bring Guava to the table.

Just import classes from org.apache.commons.collections4 instead of org.apache.commons.collections:

import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;

private MultiValuedMap<String, Employee> nameLookup = new ArrayListValuedHashMap<>();

http://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiMap.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.