ConcurrentSkipListMap Sorted Lookups
ConcurrentSkipListMap keeps keys sorted under concurrent access: ceiling, floor, and subMap range lookups work without external synchronization.
Code
import java.util.concurrent.ConcurrentSkipListMap;
public class SlidingMetrics {
public static void main(String[] args) {
// A concurrent map whose keys stay in natural order: range queries need no prior sort
ConcurrentSkipListMap<Integer, String> events = new ConcurrentSkipListMap<>();
events.put(1030, "checkout");
events.put(1005, "login");
events.put(1020, "search");
events.put(1045, "payment");
System.out.println("first: " + events.firstEntry());
System.out.println("last: " + events.lastEntry());
System.out.println("ceiling 1015: " + events.ceilingEntry(1015));
System.out.println("floor 1015: " + events.floorEntry(1015));
System.out.println("window 1010-1040: " + events.subMap(1010, true, 1040, true));
System.out.println("head until 1020: " + events.headMap(1020, true).keySet());
}
}What the code does
ConcurrentSkipListMap Sorted Lookups keeps everything in the class SlidingMetrics and the function main.
The try-with-resources form guarantees each declared resource is closed even when the body throws.
Applying it
The demo output is labeled (first:, last:, ceiling 1015:), so each line traces straight back to the statement that printed it.
Statements that matter
ConcurrentSkipListMap<Integer, String> events = new ConcurrentSkipListMap<>();
events.put(1030, "checkout");
events.put(1045, "payment");
Interactive Tools for This Topic
FAQ
- What does ConcurrentSkipListMap Sorted Lookups print when run?
- Execute the whole file; each printed line is prefixed with a label like `first:`, `last:`, `ceiling 1015:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.