按值对并行哈希映射进行排序

编程入门 行业动态 更新时间:2024-10-26 15:23:11
本文介绍了按值对并行哈希映射进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是java的新手。 我想按值排序并发哈希映射。我在这里找到了一种方法 - 按值排序并发映射条目

有更简单的方法吗?有人可以用一个例子解释一下吗?

谢谢。

解决方案

另一个解决方案是切换到使用 ConcurrentSkipListMap 在Java 6中添加。引用Javadocs:

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

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

我只是希望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 ?

Thanks.

解决方案

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

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 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.

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

更多推荐

按值对并行哈希映射进行排序

本文发布于:2023-11-16 14:21:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605069.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!