简体   繁体   English

Java并发映射按值排序

[英]java concurrent map sorted by value

I'm looking for a way to have a concurrent map or similar key->value storage that can be sorted by value and not by key. 我正在寻找一种具有并发映射或类似键->值存储的方法,该存储可以按值而不是按键排序。

So far I was looking at ConcurrentSkipListMap but I couldn't find a way to sort it by value (using Comparator ), since compare method receives only the keys as parameters. 到目前为止,我一直在看ConcurrentSkipListMap,但是找不到通过值对它进行排序的方法(使用Comparator ),因为compare方法仅接收键作为参数。

The map has keys as String and values as Integer. 该映射的键为字符串,值为整数。 What I'm looking is a way to retrieve the key with the smallest value(integer). 我正在寻找的是一种检索具有最小值(整数)的键的方法。

I was also thinking about using 2 maps, and create a separate map with Integer keys and String values and in this way I will have a sorted map by integer as I wanted, however there can be more than one integers with the same value, which could lead me into more problems. 我也在考虑使用2个映射,并使用Integer键和String值创建一个单独的映射,这样,我将按需要创建一个按整数排序的映射,但是可以有多个具有相同值的整数,可能会导致我遇到更多问题。

Example

"user1"=>3 "user2"=>1 "user3"=>3 “ user1” => 3“ user2” => 1“ user3” => 3

sorted list: "user2"=>1 "user1"=>3 "user3"=>3 排序列表:“ user2” => 1“ user1” => 3“ user3” => 3

Is there a way to do this or are any 3rd party libraries that can do this? 有没有办法做到这一点,或者任何第三方库都可以做到这一点?

Thanks 谢谢

To sort by value where you can have multiple "value" to "key" mapping, you need a MultiMap . 要按值排序,在其中可以有多个“值”到“键”的映射,您需要一个MultiMap This needs to be synchronized as there is no concurrent version. 由于没有并发版本,因此需要同步。

This doesn't meant the performance will be poor as that depends on how often you call this data structure. 这并不意味着性能会变差,这取决于您调用此数据结构的频率。 eg it could add up to 1 micro-second. 例如,它可能总计1微秒。

The principle of a ConcurrentMap is that it can be accessed concurrently - if you want it sorted at any time, performance will suffer significantly as that map would need to be fully synchronized (like a hashtable), resulting in poor throughput. ConcurrentMap的原理是可以并发访问-如果您希望随时对其进行排序,则性能将受到极大影响,因为该映射需要完全同步(如哈希表),从而导致吞吐量不佳。

So I think your best bet is to return a sorted view of your map by putting all elements in an unmodifiable TreeMap for example (although sorting a TreeMap by values needs a bit of tweaking). 因此,我认为最好的选择是通过将所有元素放入不可修改的TreeMap中来返回地图的排序视图(尽管按值对TreeMap进行排序需要一些调整)。

I recently had to do this and ended up using a ConcurrentSkipListMap where the keys contain a string and an integer. 我最近不得不这样做,最终使用了ConcurrentSkipListMap ,其中的键包含一个字符串和一个整数。 I ended up using the answer proposed below. 我最终使用了下面提出的答案。 The core insight is that you can structure your code to allow for a duplicate of a key with a different value before removing the previous one. 核心见解是,您可以对代码进行结构化,以允许在删除前一个密钥之前使用具有不同值的密钥重复项。

Atomic way to reorder keys in a ConcurrentSkipListMap / ConcurrentSkipListSet? 以原子方式对ConcurrentSkipListMap / ConcurrentSkipListSet中的键进行重新排序?

The problem was to keep a dynamic set of strings which were associated with integers that could change concurrently from different threads, described below. 问题是要保留一组动态字符串,这些字符串与可能从不同线程同时更改的整数关联,如下所述。 It sounds very similar to what you wanted to do. 这听起来与您想做的非常相似。

Is there an embeddable Java alternative to Redis? Redis是否有可嵌入的Java替代方案?

Here's the code for my implementation: 这是我的实现代码:

https://github.com/HarvardEconCS/TurkServer/blob/master/turkserver/src/main/java/edu/harvard/econcs/turkserver/util/UserItemMatcher.java https://github.com/HarvardEconCS/TurkServer/blob/master/turkserver/src/main/java/edu/harvard/econcs/turkserver/util/UserItemMatcher.java

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

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