简体   繁体   中英

Java Concurrent HashMap

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

If you need a Sorted ConcurrentMap look at 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
containsKey, get, put and remove operations and their variants.

A ConcurrentHashMap is still a Map . So you can create a new TreeMap like this:

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

First I would like to point you. you should learn to read the java SDK documentation .

Like Tangens said, and the 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(...));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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