Concurrent collections question 72 of 180 mid
What is ConcurrentSkipListMap for, and how does it differ from ConcurrentHashMap?
- It is a faster hash map
- Correct: It is a sorted, navigable concurrent map with O(log n) operations, which a hash map cannot provide
- It is the concurrent version of
LinkedHashMap - It allows null keys
When you need ordering, range queries, firstKey, headMap or ceilingEntry from several threads,
there is no concurrent TreeMap. ConcurrentSkipListMap fills that gap with a skip list, which is a
probabilistic balanced structure that can be updated with CAS rather than the rotations a red-black
tree would need under a lock.
The trade-off is the usual one: O(log n) instead of O(1) and worse constants, so do not use it as a
general-purpose map. Since Java 9 its size() reads a LongAdder, so it is cheap; on Java 8 it walked the whole list.
Quiz yourself on Concurrent collections Suggest a correction