Quick revision
The short answer
ConcurrentSkipListMap is the JDK's sorted, thread-safe NavigableMap. It is built on a lock-free skip list, so get, put and remove take O(log n) expected time and many threads can read and write at the same time, whereas a synchronized TreeMap forces every access, even a read, through one lock. Its caveats are that iterators and range views are weakly consistent, that bulk operations such as putAll or clear are not atomic, that size() is not a cheap exact count under concurrent change (older JDKs walk the whole list; recent ones keep an approximate counter, and range views still walk), and that null keys and values are not allowed.
What to remember
In this answer, we saw that ConcurrentSkipListMap is a sorted, concurrent NavigableMap built on a lock-free skip list with O(log n) expected operations, letting reads and writes proceed together where a synchronized TreeMap serializes every access through one lock. Its caveats are weakly consistent iterators and range views, bulk operations that are not atomic, a size() that is either a full traversal on older JDKs and range views or an approximate counter on recent JDKs, and no null keys or values.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ConcurrentSkipListMap is the JDK's sorted, thread-safe NavigableMap. It is built on a lock-free skip list, so get, put and remove take O(log n) expected time and many threads can read and write at the same time, whereas a synchronized TreeMap forces every access, even a read, through one lock. Its caveats are that iterators and range views are weakly consistent, that bulk operations such as putAll or clear are not atomic, that size() is not a cheap exact count under concurrent change (older JDKs walk the whole list; recent ones keep an approximate counter, and range views still walk), and that null keys and values are not allowed.
Why this question is asked
Many problems need a map that is both sorted and shared: an order book keyed by price, a schedule keyed by time, a leaderboard, a sliding window of recent events. In single-threaded code we reach for TreeMap, and the naive way to share it is Collections.synchronizedSortedMap. Interviewers ask about ConcurrentSkipListMap because it is the concurrent answer, and because its differences from TreeMap, especially around size and iteration, catch people out. In this answer, we will recap what a skip list is, see how the map achieves concurrency, compare it with a synchronized TreeMap, and go through its caveats one by one.
1A Quick Recap of Skip Lists
A skip list is a sorted linked list with express lanes. The bottom level holds every entry in key order. Above it, a sparser level links roughly every second entry, above that every fourth, and so on. Which entries get express lanes is decided by a coin flip when they are inserted, so on average each level holds half the entries of the level below.
To find a key, we start at the top level and move right while the next key is smaller than ours, then drop down a level and repeat. Each level halves the remaining distance, so a search takes about log n steps, like a balanced tree, but without ever rebalancing anything. The randomness replaces the rotations a red-black tree like TreeMap uses.
| Level | Nodes visited | Action |
|---|---|---|
| 2 | 10 | Next is 70, too big: go down |
| 1 | 10, 40 | Next is 70, too big: go down |
| 0 | 40, 50, 60 | Found 60 |
2How the Concurrent Version Works
ConcurrentSkipListMap, written by Doug Lea for java.util.concurrent, links its nodes with references that are updated using compare-and-set, an atomic CPU instruction that changes a reference only if it still holds the expected value. Inserting a key means linking one new node into the bottom list with a single compare-and-set, and then, separately, adding its index nodes in the levels above. Removing marks the node as deleted first and unlinks it afterwards, so other threads can recognise and skip half-removed nodes.
No thread ever locks the map. If two threads try to insert next to each other at the same moment, one compare-and-set succeeds and the other fails and retries from a nearby node. Most operations touch only a few nodes, so threads working on different key ranges almost never interfere. That is why it is described as lock-free with O(log n) expected time for get, put, remove, and navigation methods like ceilingKey or firstEntry.
3Comparing It With a Synchronized TreeMap
Collections.synchronizedSortedMap(new TreeMap<>()) wraps every method in one lock. A get from one thread blocks a get from another, and a put blocks everything. It also requires us to hold the lock manually while iterating, or risk ConcurrentModificationException. On a machine with 32 cores, the map runs at the speed of one.
With ConcurrentSkipListMap, reads proceed in parallel with each other and with writes, and iteration needs no external lock. TreeMap still wins in single-threaded code: it uses less memory per entry and has more predictable per-operation cost, because it has no index nodes and no retries.
4The Caveats
First, iterators and range views such as headMap, tailMap and subMap are weakly consistent. They never throw ConcurrentModificationException and never return an entry twice, but entries added or removed during the iteration may or may not appear. A report built by iterating gives us an approximate picture of a moving map, not a snapshot.
Second, bulk operations are not atomic. putAll inserts entries one by one, and clear or headMap(k).clear() removes them one by one, so other threads can see a half-finished bulk change. If several entries must change together, we need a different design, such as replacing an immutable sorted snapshot.
Third, size() is not a cheap exact count. For years the Javadoc warned that it traverses all elements, an O(n) operation. Newer JDKs, including 17, keep an internal LongAdder counter for the map itself, so size() is fast but only approximate while other threads change the map, and size() on a range view such as subMap still walks the range. Either way, prefer isEmpty() when that is the real question, and never use size() as the guard of a hard limit. Check your version if the exact cost matters.
Fourth, null keys and values are not allowed, because methods like get return null to mean absent. Keys must also be Comparable, or we pass a Comparator, and that comparison must be consistent with equals or lookups will behave strangely.
5Follow-up questions
Is there a concurrent sorted set as well?
Yes, ConcurrentSkipListSet, which is backed by a ConcurrentSkipListMap. It offers the same O(log n) concurrent operations, weakly consistent iteration and navigation methods like ceiling and floor.
When would you still choose a synchronized TreeMap?
When we need atomic multi-step operations, such as reading the first entry and removing it together with something else, and we are willing to hold one lock for them. Or when contention is low and memory matters more than throughput. For simple poll-the-smallest needs, pollFirstEntry on ConcurrentSkipListMap is already atomic.
Is ConcurrentSkipListMap.computeIfAbsent atomic like in ConcurrentHashMap?
No. Its Javadoc states the function is not guaranteed to be applied once atomically, so two threads may both run it and only one result is kept. Mapping functions must be side-effect free when used with this map.
6Conclusion
In this answer, we saw that ConcurrentSkipListMap is a sorted, concurrent NavigableMap built on a lock-free skip list with O(log n) expected operations, letting reads and writes proceed together where a synchronized TreeMap serializes every access through one lock. Its caveats are weakly consistent iterators and range views, bulk operations that are not atomic, a size() that is either a full traversal on older JDKs and range views or an approximate counter on recent JDKs, and no null keys or values.
Open with
ConcurrentSkipListMap is the JDK's concurrent sorted map. It implements NavigableMap, so I get firstKey, ceilingKey, headMap and so on, and it's built on a skip list: a sorted linked list with randomly chosen express lanes above it, so searches take about log n steps without any rebalancing.
Then, if they want more
Updates are done with compare-and-set on node links rather than locks, so it's lock-free and get, put and remove are O(log n) expected. Compared with a synchronized TreeMap, the big difference is concurrency. The synchronized wrapper puts one lock around every call, so even two readers block each other, and I'd have to hold that lock to iterate safely.
With the skip list map, readers and writers proceed in parallel and iteration needs no lock. The caveats: iterators and range views are weakly consistent, so they never throw ConcurrentModificationException but may or may not reflect concurrent changes.
Bulk operations like putAll or clearing a headMap aren't atomic; other threads can see them half done. size isn't a cheap exact count; older JDKs traverse the whole list, and newer ones keep an approximate counter, though range views still traverse, so I'd use isEmpty where I can and never rely on size for limits.
And it doesn't allow null keys or values. In single-threaded code, TreeMap is still better, since it uses less memory and has more predictable costs.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is ConcurrentSkipListMap, how does it compare with a synchronized TreeMap, and what...": https://scrivio-interview-library.netlify.app/java/concurrent-collections/cb7e7ca688/