简体   繁体   English

Java Concurrent HashMap

[英]Java Concurrent HashMap

我想将ConcurrentHashMap转换为TreeMap。我能这样做吗?

If you need a Sorted ConcurrentMap look at ConcurrentSkipListMap . 如果需要Sorted ConcurrentMap,请查看ConcurrentSkipListMap Considering its complexity it is both non blocking and fast. 考虑到它的复杂性,它既无阻塞又快速。 To be more specific: 更加具体:

This class implements a concurrent variant of SkipLists providing expected average log(n) time cost for the 此类实现了SkipLists的并发变体,提供了预期的平均log(n)时间成本
containsKey, get, put and remove operations and their variants. containsKey,get,put和remove操作及其变体。

A ConcurrentHashMap is still a Map . ConcurrentHashMap仍然是Map So you can create a new TreeMap like this: 所以你可以像这样创建一个新的TreeMap

ConcurrentHashMap myMap;
...
TreeMap myTreeMap = new TreeMap( myMap );

First I would like to point you. 首先,我想指出你。 you should learn to read the java SDK documentation . 你应该学习阅读java SDK 文档

Like Tangens said, and the TreeMap API: 像Tangens所说,和TreeMap API一样:

ConcurrentHashMAp myMap;
new TreeMap(myMap);

" Note that this implementation is not synchronized . If multiple threads access a map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally" 请注意,此实现不同步 。如果多个线程同时访问映射,并且至少有一个线程在结构上修改了映射,则必须在外部进行同步”

SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));

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

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