本文介绍了Google Guava TreeMultimap-根据排序顺序检索值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一些需要对<Destination -> Quantity>映射的值进行排序的事情,例如:

I'm trying to implement something that requires sorting of values of a <Destination -> Quantity> map, such as:

<San Francisco -> 10, Seattle -> 20, LA -> 10}

这些值不一定是不同的.我希望能够检索具有最大值和最小值的键/值对,类似于TreeMap可以对键进行的操作.

The values are not necessarily distinct. I want to be able to retrieve the key/value pair with the largest and smallest value, similar to what a TreeMap is able to do with keys.

我找到了Google Guava的TreeMultimap,它是Multimap的实现,其键和值通过其自然顺序或提供的比较器进行排序.问题是,TreeMultimap似乎没有让我根据键或值的顺序检索键/值对的任何方法(例如pollFirstEntry()在TreeMap中的操作).

I found Google Guava's TreeMultimap, which is an implementation of Multimap whose keys and values are ordered by their natural ordering or by supplied comparators. The thing is, TreeMultimap doesn't seem to have any methods that let me retrieve key/value pairs based on the ordering of the keys or values, (like what pollFirstEntry(), for instance, does in TreeMap).

就此而言,我对如何使TreeMultimap获得此功能有些困惑.我觉得它应该可以工作,也许只是我如何实例化对象的问题?

To this extent, I'm a little confused on how to make a TreeMultimap obtain this functionality. I feel like it should work, maybe it's just a matter of how I instantiate the object?

推荐答案

TreeMultimap不会以您想要的方式来支持此功能.

TreeMultimap isn't going to support this in quite the way you're looking for, I'm fairly certain.

您可以做的是TreeMultimap<Integer, String>-交换键和值的作用-然后TreeMultimap.asMap().lastEntry()将为您提供一个Map.Entry<Integer, Collection<String>>,对应于最大的Integer和所有

What you could do is a TreeMultimap<Integer, String> -- swapping the role of the keys and the values -- and then TreeMultimap.asMap().lastEntry() would get you a Map.Entry<Integer, Collection<String>>, corresponding to the greatest Integer and all the Strings associated with it.

TreeMultimap绝对不支持的一件事是将 all 键的组合值视为单个排序的集合. (当然,您可以将它们视为带有values()未排序集合).

The one thing definitely not supported by TreeMultimap is looking at the combined values for all keys as a single sorted collection. (You can look at them as an unsorted collection with values(), of course.)

这篇关于Google Guava TreeMultimap-根据排序顺序检索值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 11:37