Reputation: 6774
i want to convert a ConcurrentHashMap to a TreeMap.Can i do that?
Upvotes: 3
Views: 2031
Reputation: 40266
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.
Upvotes: 6
Reputation: 61793
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(...));
Upvotes: 2
Reputation: 39753
A ConcurrentHashMap
is still a Map
. So you can create a new TreeMap
like this:
ConcurrentHashMap myMap;
...
TreeMap myTreeMap = new TreeMap( myMap );
Upvotes: 5