In Java, ConcurrentHashMap
is there for better multithreading
solution. Then when should I use ConcurrentSkipListMap
? Is it a redundancy?
Does multithreading aspects between these two are common?
Methods of ConcurrentSkipListMap. Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such entry. Returns the least key greater than or equal to the given key, or null if there is no such key. Removes all of the mappings from this map.
The ConcurrentSkipListSet class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractSet class. It provides a scalable and concurrent version of NavigableSet in Java.
ConcurrentHashMap are sorted, not ordered like LinkedHashMap are and, I believe, can't thus be the required concurent alternative to a LinkedHashMap.
These two classes vary in a few ways.
ConcurrentHashMap does not guarantee* the runtime of its operations as part of its contract. It also allows tuning for certain load factors (roughly, the number of threads concurrently modifying it).
ConcurrentSkipListMap, on the other hand, guarantees average O(log(n)) performance on a wide variety of operations. It also does not support tuning for concurrency's sake. ConcurrentSkipListMap
also has a number of operations that ConcurrentHashMap
doesn't: ceilingEntry/Key, floorEntry/Key, etc. It also maintains a sort order, which would otherwise have to be calculated (at notable expense) if you were using a ConcurrentHashMap
.
Basically, different implementations are provided for different use cases. If you need quick single key/value pair addition and quick single key lookup, use the HashMap
. If you need faster in-order traversal, and can afford the extra cost for insertion, use the SkipListMap
.
*Though I expect the implementation is roughly in line with the general hash-map guarantees of O(1) insertion/lookup; ignoring re-hashing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With