简体   繁体   English

检索/修改k / v对图中的最高和最低值?

[英]Retrieving/modifying highest & lowest values in k/v pair map?

So I have encountered the situation where I have ak/v map of destination to quantity, ie {San Francisco -> 10}, and I want to retrieve the k/v pair with the highest or lowest value, and then after retrieving it, I want to modify the value. 因此,我遇到了这样的情况:我有一个目的地/数量的ak / v映射,即{San Francisco-> 10},我想检索具有最高或最低值的k / v对,然后在检索它之后,我要修改值。 TreeMap obviously has this functionality, but for the keys, not the values. TreeMap显然具有此功能,但对于键而不是值。 Are there any packages or other ways to achieve this functionality? 是否有任何软件包或其他方式可以实现此功能? Thanks. 谢谢。

您可以使用来自Google番石榴的BiMap。

Key/value maps are inherently unordered in most implementations (because they are implemented using hash tables). 键/值映射在大多数实现中本质上是无序的(因为它们是使用哈希表实现的)。 You'd need to iterate over all the values to determine the maximum. 您需要遍历所有值以确定最大值。 You should consider using a tuple with both the key and value, and then insert the tuple in a data structure that supports ordering. 您应该考虑同时使用具有键和值的元组,然后将元组插入支持排序的数据结构中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM