Is there a reason to sort MultiMap in Java and how to do that?
-
1I propose this question be reopened – mdec Nov 05 '08 at 07:45
-
I also propose it be reopend. – Steve McLeod Nov 05 '08 at 07:54
-
What is not real about "How do I sort a MultiMap
in java?"? I stick with mdec and Steve. +1 for reopening! – Burkhard Nov 05 '08 at 08:01 -
There must be some way to vote down the closure of a question. not the question it's self, but if say 5 people vote the closure down the question is re-opened and the person who closes it faces a rep penalty. – Ron Tuffin Nov 05 '08 at 08:33
-
In Pax's defence, the original unedited question did deserve to be closed. – mdec Nov 05 '08 at 10:38
-
I re-opened it - it _is_ a programming question, though it's not a good one. swathi, would you please tell us what you've tried so far? If you can write example code to setup a multimap
that needs to be sorted then someone may be able to help you out more easily. – Adam Davis Nov 07 '08 at 03:28 -
I don't know java well enough to close this as an exact duplicate, but it appears to be the same as http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java -Adam – Adam Davis Nov 07 '08 at 03:26
-
The linked question is about sorting the values of a map, this is about sorting a multimap (key -> collectionOfValues).. I think. – SCdF Nov 07 '08 at 05:59
3 Answers
Assuming you're talking about org.apache.commons.collections.MultiMap
, then you can't; since it returns a Collection
and not a List
, it doesn't support the concept of order.
If you are talking about org.apache.commons.collections.MultiHashMap
, then all you need to do is iterate over the keys, take the ArrayList
returned and sort it using Collections.sort()
.
That's assuming you are using that implementation though.
There is nothing stopping you from implementing your own MultiMap
fairly easily though, that supports sorting lists. It may be as easy as HashMap<K, Collection<V>>
, I'm not familiar with how MultiMaps work.
Actually I don't know why you would want to sort a Map. A Map is a dictionary and you retrieve from this dictionary a (or in the case of multimaps a collection of) value(s) you're interested in.
In the case of a MultiMap you perhaps would want to sort the Collection resulting from a get. But what advantage do you have by a sorted Map since it doesn't speed up anything in finding a specific value?

- 24,189
- 7
- 34
- 49
-
If you think of a Map as a collection of tuples, this problem makes sense. Maps in Java are a way to assign an index to every instance in a collection. Think of Maps as tables with a non-syntactical PK, for example. 1->orange, 2->banana The problem is when you start using maps as **collections of tuples**. George->Constanza, Jerry->Seinfeld. Then you may need to sort the map, say, by First name or last name (key or value) If Java provided tuples 'alla' Python, this wouldn't be a problem – llappall Sep 21 '11 at 18:47
-
I don't understand the comment. Either one iterates over the complete Map, or one would retrieve a value with a key. In both cases no sorting is needed. I think your example as collection of tuples is valid, but I wouldn't use a Multimap for this. Furthermore I would use another datastructure for your tuple example. – boutta Sep 27 '11 at 06:14
Easiest solution is to use TreeMultimap from guava. Use either directly
TreeMultimap<...> sortedMap = TreeMultimap.create(notSortedMultiMap);
if your keys and values are sortable naturally (implement Comparable), or
TreeMultimap<...> sortedMap = TreeMultimap.create(keyComparator, valueComparator);
sortedMap.putAll(notSortedMultiMap);
if you need to provide custom comparators.
If you know you will need it sorted and not care about retrieval speed that much, you can of course use TreeMap from very start.
Then you can iterate over TreeMap or use something like values() or entries to get sorted collection.

- 3,595
- 2
- 13
- 18