Skip to content

Commit 2d188e6

Browse files
committed
feat: add ConcurrentSkipListMapDemo with basic example
What - Introduced `ConcurrentSkipListMapDemo` class. - Demonstrated use of `ConcurrentSkipListMap` via its `NavigableMap` interface. - Inserted a few entries and printed them in natural sorted order. Why - Complements `ConcurrentHashMapDemo` by showing a concurrent map that preserves ordering. - Highlights a scalable, thread-safe alternative to `TreeMap` when ordering is needed. - Provides a simple reference for concurrency + ordering in collections. How - Created a `ConcurrentSkipListMap<Integer, String>`. - Populated it with keys `2,1,3`. - Printed the map to show that entries are maintained in ascending key order. Logic - `ConcurrentSkipListMap` internally uses a skip list to maintain sorted keys. - Iteration order is sorted by natural ordering or provided comparator. - Thread-safe without needing explicit synchronization. Real-world applications - Suitable for concurrent caches or lookup tables requiring both fast access and sorted keys. - Useful for range queries (e.g., `headMap`, `tailMap`) in multithreaded contexts. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 646c075 commit 2d188e6

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.NavigableMap;
2+
import java.util.concurrent.ConcurrentSkipListMap;
3+
4+
/**
5+
* Small demo class to complement {@link ConcurrentHashMapDemo}.
6+
7+
* <p>
8+
* ConcurrentSkipListMap provides a thread-safe sorted map (ordered by natural ordering
9+
* or provided Comparator). It is a scalable concurrent alternative to TreeMap when you
10+
* need both ordering and concurrency.
11+
* </p>
12+
13+
* <p>
14+
* This class provides a minimal stand-in for documentation purposes and for the
15+
* {@code @see} reference in {@link ConcurrentHashMapDemo}. It intentionally contains
16+
* only a short example that prints a small concurrent, sorted map.
17+
* </p>
18+
*/
19+
20+
public class ConcurrentSkipListMapDemo {
21+
public static void main(String[] args) {
22+
// Create a thread-safe, sorted map backed by a skip list.
23+
NavigableMap<Integer, String> skipListMap = new ConcurrentSkipListMap<>();
24+
25+
// Populate with a few entries (kept simple for demonstration).
26+
skipListMap.put(2, "Two");
27+
skipListMap.put(1, "One");
28+
skipListMap.put(3, "Three");
29+
30+
// Prints the map (will be in sorted key order: 1, 2, 3).
31+
System.out.println(skipListMap);
32+
}
33+
}

Section25CollectionFramework/src/ConcurrentMap/ConcurrentSkipListMapDemo/ConcurrentSkipListMapDemo.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)