有更简单的方法吗?有人可以用一个例子解释一下吗?
谢谢。
解决方案另一个解决方案是切换到使用 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.
更多推荐
按值对并行哈希映射进行排序
发布评论