对番石榴BiMap进行排序 [英] Sorting a Guava BiMap
问题描述
我是Guava库的新手,但发现BiMap非常适合我的需求.我的问题是我需要对BiMap中的值进行排序.通常,由于它的继承排序,我本来会使用TreeMap,但是必须反转地图以查看键,就好像它们是值一样.
I am new to the Guava library, but have found that the BiMap fits my needs nicely. My one issue is that I need to sort the values in my BiMap. Normally, I would have used a TreeMap due to its inherit sorting, but being able to invert the map to look at the keys as if they are values is a must.
因此,我一直在寻求有关此问题的专家意见.我看到的解决方案是: 1.创建一个BiMap比较器,并使用我自己的地图排序实用程序. 2.建立我自己的"BiTreeMap"数据结构.
So I was looking for advice from you experts about going about this. The solutions I see are: 1. Create a BiMap comparator and use my own map sorting utility. 2. Build my own 'BiTreeMap' data structure.
有没有考虑过的解决方案吗?有没有我错过的番石榴方式"?谢谢!
Is there a solution I haven't considered? Is there a 'Guava way' to do this that I missed? Thanks!
推荐答案
我们目前尚无排序的BiMap
类型,因为它的工作方式有点模棱两可:条目会按键排序吗?通过价值观?请问正向条目是否按键排序,反向条目是否按值排序?
We don't currently have a sorted BiMap
type because it's a little ambiguous how it would work: would the entries be sorted by the keys? By the values? Would the forward entries be sorted by the keys, and the inverse entries be sorted by the values?
当前,唯一真正可用的替代方法是ImmutableBiMap
,它保留了您将条目放在其中的顺序-因此ImmutableBiMap.copyOf(Maps.newTreeMap(map))
会为您提供按键排序的BiMap
.
Currently, the only real available alternative is ImmutableBiMap
, which retains the ordering that you put the entries in -- so ImmutableBiMap.copyOf(Maps.newTreeMap(map))
would give you a BiMap
sorted by the keys.
(不过,如果您提出了具有使用案例更多详细信息的功能请求,我们可能会更认真地考虑提供直接排序的BiMap
.)
(We might more seriously consider providing a directly sorted BiMap
if you filed a feature request with more details on your use case, though.)
这篇关于对番石榴BiMap进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!