views:

141

answers:

2

I need a collection that behaves something like C++ multimap, but I also need to be able to get elements by a range of keys.

+2  A: 

There is no built-in multimap collection in Java. To solve this you can map to every key a list of values: Map<String, List<String>>, for example. Otherwise there are third-party libraries with implemented multimaps - here is one of them.

Petar Minchev
+4  A: 

You can look into Google Collections. It has multiple implementations for MultiMap.

Matt Dearing
In particular. the Google Collections TreeMultimap class includes an asMap() method returning a SortedMap. You can then call methods like SortedMap.subMap() to retrieve mappings for a range of keys.
Jared Levy