Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java. Sorted map by value [duplicate]

Possible Duplicate:
How to sort a Map<Key, Value> on the values in Java?

I need sorted map like TreeMap but sorted by value. My map will be huge, so i can't just sort my map anytime i need. Exist any good solution to resolve this problem? Maybe exist external jar who meets this?

like image 324
user1711160 Avatar asked Nov 12 '22 17:11

user1711160


1 Answers

There are a number of ways to satisfy your requirement. As you have subsequently clarified that you may have duplicate objects in your current TreeMap, perhaps you could replace your TreeMap with a third-party multimap (Guava, Apache Commons Collections), then swap your keys and values around - i.e. replace TreeMap<Key, Value> with Multimap<Value, Key>. Depending on the details of your situation I believe this stands a good chance of working for you.

like image 56
Mark A. Fitzgerald Avatar answered Dec 22 '22 16:12

Mark A. Fitzgerald