Jony
Jony

Reputation: 6774

Java Concurrent HashMap

i want to convert a ConcurrentHashMap to a TreeMap.Can i do that?

Upvotes: 3

Views: 2031

Answers (3)

John Vint
John Vint

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

Alfred
Alfred

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

tangens
tangens

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

Related Questions