C

Core Java tutorial for beginners

Clean • Professional

ConcurrentSkipListMap in Java – Thread-Safe Sorted Map & Examples

3 minute

ConcurrentSkipListMap in Java

ConcurrentSkipListMap is a high-performance, thread-safe, sorted map in Java. It is part of the java.util.concurrent package and serves as the concurrent alternative to TreeMap. It maintains ascending order of keys and provides non-blocking, scalable operations using a Skip List internally.


What is ConcurrentSkipListMap?

A ConcurrentSkipListMap is:

  • A thread-safe sorted map
  • Based on a Skip List instead of a tree or hash table
  • Mostly lock-free, using non-blocking Compare-And-Swap (CAS) operations
  • Provides logarithmic time complexity for get(), put(), and remove()
  • Maintains keys in ascending order
  • Implements:
    • ConcurrentNavigableMap
    • NavigableMap
    • SortedMap
    • Map

Why Use ConcurrentSkipListMap?

Use ConcurrentSkipListMap when you need:

  • Sorted keys with high concurrency
  • Concurrent reads and writes without locking the entire map
  • Fail-safe iteration during concurrent modifications
  • A sorted alternative to ConcurrentHashMap

Avoid it when:

  • You need unsorted map → use ConcurrentHashMap
  • You need insertion-order map → use LinkedHashMap
  • You have extremely high write frequencyConcurrentHashMap may be better

Internal Working (Skip List)

A Skip List is like a multi-level linked list:

  • Level 0 → standard linked list
  • Higher levels → shortcuts for faster search
  • Searching jumps through levels → O(log n) time complexity
  • Updates (put/remove) use lock-free CAS operations
  • Readers can access the map without blocking

Benefits:

  • High concurrency
  • Lock-free for reads
  • Scalable and efficient under heavy load

Hierarchy

Map
 └─ SortedMap
     └─ NavigableMap
         └─ ConcurrentNavigableMap
             └─ ConcurrentSkipListMap

Key Features of ConcurrentSkipListMap

FeatureDescription
Thread-safeAllows safe concurrent access without external synchronization
Sorted MapKeys automatically stored in ascending order
Skip List basedFast log-time operations
Non-blockingCAS-based lock-free updates
Fail-safe iteratorsIterators do not throw exceptions
Navigable operationsfloorKey(), ceilingKey(), higherKey() etc.
No null keys/valuesNulls not allowed (same as TreeMap)

Example: Basic Usage

import java.util.concurrent.ConcurrentSkipListMap;

public class Example {
    public static void main(String[] args) {
        ConcurrentSkipListMap<Integer, String> map = new ConcurrentSkipListMap<>();

        map.put(30, "Java");
        map.put(10, "Spring");
        map.put(20, "Hibernate");

        System.out.println(map); // {10=Spring, 20=Hibernate, 30=Java}
    }
}
  • Maintains ascending key order
  • Thread-safe without manual synchronization

Navigation Methods Example

System.out.println(map.firstKey());     // 10
System.out.println(map.lastKey());      // 30
System.out.println(map.higherKey(20));  // 30
System.out.println(map.lowerKey(20));   // 10

Concurrent Iteration (Fail-Safe)

for (var entry : map.entrySet()) {
    System.out.println(entry);
    map.put(40, "Microservices"); // No ConcurrentModificationException
}
  • Iterators are fail-safe
  • Iterators reflect latest updates safely

Why Null Keys Are Not Allowed?

  • Null keys make ordering impossible
  • Comparing null with non-null would throw errors
  • TreeMap also disallows null keys for the same reason

ConcurrentSkipListMap vs ConcurrentHashMap

FeatureConcurrentSkipListMapConcurrentHashMap
SortingSorted (ascending)Unordered
Internal StructureSkip ListHash Table
Navigation MethodsYesNo
PerformanceSlightly slower due to sortingFaster
Use CaseOrdered + concurrentHigh-speed access without ordering

ConcurrentSkipListMap vs TreeMap

FeatureTreeMapConcurrentSkipListMap
Thread-safeNoYes
Internal StructureRed-Black TreeSkip List
ConcurrencyNeeds external syncBuilt-in concurrency
Performance under loadPoorExcellent

When to Use ConcurrentSkipListMap

  • Sorted keys required
  • High-performance concurrency
  • Lock-free operations with ordering
  • Multi-threaded apps: servers, microservices, schedulers

Use Cases:

  • Leaderboards
  • Event log timestamps
  • Real-time dashboards
  • Job schedulers
  • Concurrent caches where order matters

When NOT to Use ConcurrentSkipListMap

Avoid it when:

  • You do NOT need sorted keys → use ConcurrentHashMap
  • You need insertion order → use LinkedHashMap
  • You want maximum write performance → CHM is faster
  • You have low concurrency → TreeMap may be enough

Points to Remember

  • Thread-safe, sorted map
  • Uses Skip List instead of a tree
  • High concurrency + lock-free algorithms
  • No null keys or values allowed
  • Provides navigation methods
  • Iterators are fail-safe

Article 0 of 0