Home Java Collections Methods synchronizedNavigableMap() Java Collections synchronizedNavigableMap() Method Definition In Java 8 and later, Collections.synchronizedNavigableMap(m) returns a thread-safe NavigableMap wrapper. Each navigation call, such as floorEntry, ceilingEntry, lastEntry, or pollFirstEntry, holds the same lock.
Multi-step logic, like "find the smallest room that fits, then remove it", and every loop over a view must be wrapped in synchronized (map) { ... }.
A static method of the java.util.Collections utility class, available since Java 8. Call it on the class name, as in Collections.synchronizedNavigableMap(...), after import java.util.Collections;. You never create a Collections object.
Syntax Wrap a new TreeMap.
NavigableMap<Integer, String> rooms = Collections.synchronizedNavigableMap(new TreeMap<Integer, String>());
synchronized (rooms) {
Map.Entry<Integer, String> e = rooms.ceilingEntry(3);
if (e != null) rooms.remove(e.getKey());
} Call What it does firstEntry() / lastEntry()Atomic reads of the ends. pollFirstEntry() / pollLastEntry()Atomic read-and-remove. headMap(k, true) / descendingMap()Views guarded by the same lock. synchronized (map) { ... }For loops and lookup-then-change logic.
Added in Java 8. For heavy concurrent use, ConcurrentSkipListMap in java.util.concurrent is a navigable map designed for many threads.
Library declarations // java.util.Collections (Java 8+)
public static <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m) Parameters Parameter Description mThe navigable map to make thread-safe. Do not use it directly afterwards.
Return Value Return Value Description NavigableMap<K,V>A synchronized view of m.
Important
Iterating any key set, entry set, descending map, or sub-map needs synchronized (wrapper) on the original wrapper to avoid ConcurrentModificationException. A lookup followed by remove or put is not atomic unless locked together.
Compare with Similar Methods Related wrappers:
Java Examples 1. Concurrent bids 2. Find and remove 3. Inclusive sub-map Example 1 — Highest and Lowest Bid from Two Bidders Two threads place bids keyed by amount. Afterwards the extremes are read in single calls.
import java.util.Collections;
import java.util.NavigableMap;
import java.util.TreeMap;
public class CollectionsSynchronizedNavigableMapBids {
public static void main(String[] args) throws InterruptedException {
NavigableMap<Integer, String> bids = Collections.synchronizedNavigableMap(new TreeMap<Integer, String>());
Thread ana = new Thread(() -> {
for (int b = 10; b <= 50; b += 20) {
bids.put(b, "ana");
}
});
Thread ben = new Thread(() -> {
for (int b = 20; b <= 60; b += 20) {
bids.put(b, "ben");
}
});
ana.start();
ben.start();
ana.join();
ben.join();
System.out.println("bids: " + bids.size() + ", highest " + bids.lastEntry() + ", lowest " + bids.firstEntry());
}
} bids: 6, highest 60=ben, lowest 10=ana How It Works Unique amounts. Ana bids 10, 30, 50 and Ben bids 20, 40, 60, so no bid overwrites another.Locked inserts. Both threads put into the same tree safely through the wrapper.Ends as entries. lastEntry() and firstEntry() return key=value pairs: Ben's 60 is highest, Ana's 10 is lowest.Example 2 — Assign the Smallest Room That Fits ceilingEntry finds a room with enough beds; removing it must happen under the same lock.
import java.util.Collections;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class CollectionsSynchronizedNavigableMapRooms {
static String assign(NavigableMap<Integer, String> rooms, int guests) {
synchronized (rooms) {
Map.Entry<Integer, String> e = rooms.ceilingEntry(guests);
if (e == null) {
return "none";
}
rooms.remove(e.getKey());
return e.getValue() + " (" + e.getKey() + " beds)";
}
}
public static void main(String[] args) {
NavigableMap<Integer, String> rooms = Collections.synchronizedNavigableMap(new TreeMap<Integer, String>());
rooms.put(1, "101");
rooms.put(2, "102");
rooms.put(4, "201");
System.out.println("2 guests -> " + assign(rooms, 2));
System.out.println("2 guests -> " + assign(rooms, 2));
System.out.println("3 guests -> " + assign(rooms, 3));
System.out.println("left: " + rooms);
}
} 2 guests -> 102 (2 beds)
2 guests -> 201 (4 beds)
3 guests -> none
left: {1=101} How It Works Best fit. ceilingEntry(2) picks the smallest room with at least 2 beds: room 102.Upgrade. With 102 gone, the next pair gets the 4-bed room 201.Full. Only a 1-bed room remains, so 3 guests get "none". The lock keeps lookup and removal together under concurrency.Example 3 — Total Sales for Days 1 to 3 Sum an inclusive headMap under the lock and print the days in reverse.
import java.util.Collections;
import java.util.NavigableMap;
import java.util.TreeMap;
public class CollectionsSynchronizedNavigableMapSales {
public static void main(String[] args) {
NavigableMap<Integer, Integer> sales = Collections.synchronizedNavigableMap(new TreeMap<Integer, Integer>());
sales.put(1, 5);
sales.put(2, 8);
sales.put(3, 4);
sales.put(4, 9);
NavigableMap<Integer, Integer> firstThree = sales.headMap(3, true);
int total = 0;
synchronized (sales) {
for (int v : firstThree.values()) {
total += v;
}
}
System.out.println("days 1-3 total: " + total);
System.out.println("days newest first: " + sales.descendingKeySet());
}
} days 1-3 total: 17
days newest first: [4, 3, 2, 1] How It Works Inclusive bound. headMap(3, true) covers days 1, 2, and 3.Locked loop. Summing the sub-map's values holds synchronized (sales), the original wrapper: 5 + 8 + 4 = 17.Reverse keys. descendingKeySet() lists the days from newest to oldest; printing it is a single locked call.Frequently Asked Questions What does Collections.synchronizedNavigableMap() do? It returns a thread-safe NavigableMap wrapper; every call, including floorEntry and pollFirstEntry, holds one lock.
Is ceilingEntry() followed by remove() atomic? No. Wrap both in synchronized (map) { ... }.
Do sub-maps such as headMap(k, true) share the lock? Yes, but loops over them must still synchronize on the original wrapper.
Which Java version added synchronizedNavigableMap()? Java 8.
What is a concurrent alternative? java.util.concurrent.ConcurrentSkipListMap is a NavigableMap designed for concurrent use.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion