按值对并发散列映射进行排序 [英] Sorting a concurrent hash map by value

查看:137
本文介绍了按值对并发散列映射进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的java。
我想按照值排序一个并发哈希映射。我找到了一种方法 -
按值排序并发映射条目

I am new to java. I want to sort a concurrent hash map by value. I found a way of doing it here - sort concurrent map entries by value

有更简单的方法吗?有人可以用示例解释它吗?

Is there a simpler way of doing it? Can someone please explain it with an example ?

谢谢。

推荐答案

另一种解决方案是切换到使用 ConcurrentSkipListMap 在Java 6中添加。要从Javadoc引用:

Another solution would be to switch to using the ConcurrentSkipListMap which was added in Java 6. To quote from the Javadocs:


此类实现了SkipLists的并发变体,为containsKey,get,put和remove操作及其变体提供了预期的平均log(n)时间成本。插入,删除,更新和访问操作由多个线程安全地并发执行。迭代器是弱一致的,返回元素反映地图的状态在迭代器创建之后的某个点。它们不会抛出ConcurrentModificationException,并且可以与其他操作同时进行。升序键排序视图及其迭代器比降序快。

This class implements a concurrent variant of SkipLists providing expected average log(n) time cost for the containsKey, get, put and remove operations and their variants. Insertion, removal, update, and access operations safely execute concurrently by multiple threads. Iterators are weakly consistent, returning elements reflecting the state of the map at some point at or since the creation of the iterator. They do not throw ConcurrentModificationException, and may proceed concurrently with other operations. Ascending key ordered views and their iterators are faster than descending ones.

SkipLists是平衡树的概率替换。他们有相同的 O 作为树,但通常他们的实现显然更简单。如果大多数操作是哈希表查找( O(1)按定义),那么你会看到一个性能不同的体面表大小,但如果你需要经常排序,这可能是一个更好的解决方案。

SkipLists are probabilistic replacements for balanced trees. They have the same O as trees but usually their implementation is markedly simpler. If most of your operations are hash-table lookups (O(1) by definition) then you will see a performance different for decent table sizes, but if you require sorting often, this may be a better solution.

我只是希望Java提供这个伟大的数据结构的非并发版本。

I just wish Java provided a non-concurrent version of this great data structure.

这篇关于按值对并发散列映射进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆