What are ConcurrentSkipListMap and ConcurrentSkipListSet, and when would you use them?
answer
- Concurrent sorted = TreeMap/TreeSet analogues
- Backed by a skip list (layered linked lists), not a red-black tree
- Expected O(log n); lock-free via CAS, easy to make concurrent
- Navigable API: ceiling/floor/headMap/subMap range queries
- No nulls; weakly consistent iterators; size() approximate
basics
~20 sConcurrentSkipListMap and ConcurrentSkipListSet are thread-safe sorted map and set classes, the concurrent counterparts of TreeMap and TreeSet. Many threads can use them at once, and they support range queries such as 'all keys between A and B'.
solid answer
~50 sConcurrentSkipListMap and ConcurrentSkipListSet are the concurrent, sorted analogues of TreeMap and TreeSet. They keep keys/elements ordered (by natural ordering or a supplied Comparator) and are fully thread-safe without locking the whole structure, so reads and writes scale across threads. They are backed by a skip list rather than a red-black tree because a skip list is far easier to make lock-free/concurrent: it is a layered linked list where higher levels skip over many nodes, giving expected O(log n) search, insert, and delete. The map implements ConcurrentNavigableMap and the set implements NavigableSet, so you get the navigation and range operations of the sorted interfaces — firstKey, ceilingKey, floorEntry, headMap, tailMap, subMap, descendingMap — all thread-safe. Iterators are weakly consistent (no ConcurrentModificationException). You reach for them when you need a concurrently-accessed structure that also maintains sorted order or supports range/nearest-key queries; if you only need hash lookups without ordering, ConcurrentHashMap is faster.
go deeper
Knows they are thread-safe versions of TreeMap/TreeSet that keep things sorted and can be shared by many threads.
Can explain the map implements ConcurrentNavigableMap and the set NavigableSet, with range queries, that both are O(log n) and skip-list based, and when to prefer ConcurrentHashMap.
Explains why a skip list (CAS-friendly, no global rotations) is used over a red-black tree, the probabilistic balancing/height, weakly consistent iterators, no-null and approximate-size caveats.
Weighs O(log n) ordered concurrent access vs O(1) hashed access at scale, reasons about comparator cost and memory overhead of skip-list levels, and identifies range-query workloads (time-series, leaderboards) where it's the right structure.
## The need Sometimes you need a thread-safe collection that also keeps its elements **sorted** and supports **range queries** ("give me every key from 100 to 200", "the smallest key ≥ x"). `ConcurrentHashMap` is thread-safe but unordered, and `TreeMap`/`TreeSet` are sorted but *not* thread-safe. The JDK fills the gap with **ConcurrentSkipListMap** (sorted map) and **ConcurrentSkipListSet** (sorted set, internally backed by a skip-list map). ## Key terms (from scratch) - **Sorted / NavigableMap**: a map whose keys are kept in a defined order, exposing navigation methods like `firstKey()`, `lastKey()`, `ceilingKey(k)` (smallest key ≥ k), `floorKey(k)` (largest key ≤ k), and range *views* like `headMap`, `tailMap`, `subMap`. - **Natural ordering / Comparator**: keys are ordered either by their own `compareTo` (natural ordering) or by a `Comparator` you pass to the constructor. - **Red-black tree**: the balanced binary search tree that backs `TreeMap`. Balancing requires rotations that touch several nodes at once, which is hard to do concurrently without coarse locking. - **Skip list**: an alternative ordered structure. Picture a base linked list of all nodes in sorted order, with several *express* linked lists stacked on top, each skipping over more nodes (like express vs local subway lines). To search, you start at the top express lane and drop down a level whenever the next node would overshoot the target, ending at the right spot. A node's height is chosen randomly (probabilistically), which keeps the structure balanced *on average* with expected **O(log n)** search/insert/delete — without the global rebalancing rotations a tree needs. ## Why a skip list for concurrency The whole point is that a skip list is dramatically easier to make **lock-free** than a balanced tree. Inserts and deletes touch only a few `next` pointers along the search path and can be done with **CAS** operations, with no whole-tree rotation. So `ConcurrentSkipListMap` achieves scalable concurrent access (many threads reading and writing) without a global lock — that is why the JDK chose a skip list instead of a concurrent red-black tree. ## Properties and gotchas - **Interfaces**: the map implements `ConcurrentNavigableMap` and the set implements `NavigableSet` (there is no `ConcurrentNavigableSet` interface), so all the navigation and range operations are available and thread-safe, and range views (`subMap`, etc.) are live, concurrent sub-views. - **No null keys/values**: the Javadoc's reason is that some `null` return values could not be told apart from the absence of an element, for example `get` returning `null`. - **Weakly consistent iterators**: they reflect some state at/after creation, never throw `ConcurrentModificationException`, and don't promise a snapshot. - **`size()` is only an estimate under concurrent modification** — don't rely on it for control flow. `ConcurrentSkipListSet`'s Javadoc still warns that it is not a constant-time operation; at JDK 27 (and 25) a top-level map or set sums an internal `LongAdder` counter instead of walking the list, while a `subMap` or `subSet` view counts by walking its range. - **Performance**: expected O(log n) operations (vs O(1) average for hash maps). Comparisons are done via the key's `compareTo`/`Comparator`, so an inconsistent or expensive comparator hurts correctness/performance. ## ConcurrentHashMap vs ConcurrentSkipListMap — choosing - Need only key→value lookups, no ordering? Use **ConcurrentHashMap** — O(1) average, faster. - Need keys kept **sorted**, or need **range / nearest-key** queries (ceiling/floor/headMap/subMap), concurrently? Use **ConcurrentSkipListMap**. The ordering costs you the jump from O(1) to O(log n). Typical uses: concurrent leaderboards or time-ordered event indexes (range scans by timestamp), priority-by-key structures, or any place you'd want a thread-safe `TreeMap`.
- Why does the JDK back the concurrent sorted map with a skip list rather than a concurrent red-black tree?A balanced tree needs rotations that touch many nodes, which is very hard to do lock-free. A skip list inserts/deletes by CAS-ing a handful of next pointers along the search path, with random node heights for balance — far easier to make scalable and lock-free, at the same expected O(log n).
- When would you pick ConcurrentHashMap over ConcurrentSkipListMap?When you only need hash-style key lookups with no ordering or range queries: ConcurrentHashMap gives average O(1) operations and higher throughput. Use the skip-list map only when you need sorted order or navigation (ceiling/floor/subMap).
- Thinking they are O(1) like a hash map — they are O(log n)
- Saying they are backed by a red-black tree (it's a skip list, chosen for concurrency)
- Using them when you don't need ordering (ConcurrentHashMap is faster)
- Expecting size() to be exact, or iterators to be snapshots, under concurrent updates
- Trying to store null keys or values
- ConcurrentSkipListMap
- Java 1.6
- ConcurrentSkipListSet
- Java 1.6
Describes ConcurrentSkipListMap as implemented at JDK 27, unchanged from 25 LTS: size() sums an internal LongAdder counter instead of walking the list. The Javadoc promises no particular cost for it, so other releases may differ.
context
- ConcurrentSkipListMap (Java SE 27 & JDK 27)docs.oracle.com
- ConcurrentSkipListSet (Java SE 27 & JDK 27)docs.oracle.com
- ConcurrentSkipListMap.java at jdk-27-gagithub.com