问题描述
我需要像TreeMap这样排序的地图,但是按值排序.我的地图很大,所以我不能随时随地对我的地图进行排序.是否存在解决此问题的良好解决方案?也许存在外部罐子遇到这个问题?
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?
推荐答案
有很多方法可以满足您的要求.正如您随后澄清的那样,您当前的 TreeMap
中可能有重复的对象,也许您可以用第三方多重地图(番石榴, Apache Commons Collections ),然后交换您的密钥和值-即用 Multimap< Value替换
.根据您的具体情况,我相信这是为您工作的好机会. TreeMap< Key,Value>
键>
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.
这篇关于Java.按值对地图排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!