Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Map that is Multi Value and supports generics?

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

like image 705
Zombies Avatar asked Oct 14 '11 20:10

Zombies


2 Answers

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

like image 173
Peter Lawrey Avatar answered Oct 29 '22 12:10

Peter Lawrey


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);
like image 26
Matt Ball Avatar answered Oct 29 '22 14:10

Matt Ball