Java Collections synchronizedNavigableMap() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

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.

Java
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());
}
CallWhat 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
// java.util.Collections (Java 8+)
public static <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m)

Parameters

ParameterDescription
mThe navigable map to make thread-safe. Do not use it directly afterwards.

Return Value

Return ValueDescription
NavigableMap<K,V>A synchronized view of m.

Compare with Similar Methods

Related wrappers:

MethodHow it differs
synchronizedSortedMap(m)Older; no navigation methods.
unmodifiableNavigableMap(m)Read-only navigable view.
checkedNavigableMap(m, k, v)Type-checked navigable map.

Java Examples

Example 1 — Highest and Lowest Bid from Two Bidders

Two threads place bids keyed by amount. Afterwards the extremes are read in single calls.

Java
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());
    }
}

How It Works

  1. Unique amounts. Ana bids 10, 30, 50 and Ben bids 20, 40, 60, so no bid overwrites another.
  2. Locked inserts. Both threads put into the same tree safely through the wrapper.
  3. 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.

Java
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);
    }
}

How It Works

  1. Best fit. ceilingEntry(2) picks the smallest room with at least 2 beds: room 102.
  2. Upgrade. With 102 gone, the next pair gets the 4-bed room 201.
  3. 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.

Java
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());
    }
}

How It Works

  1. Inclusive bound. headMap(3, true) covers days 1, 2, and 3.
  2. Locked loop. Summing the sub-map's values holds synchronized (sales), the original wrapper: 5 + 8 + 4 = 17.
  3. Reverse keys. descendingKeySet() lists the days from newest to oldest; printing it is a single locked call.

Frequently Asked Questions

It returns a thread-safe NavigableMap wrapper; every call, including floorEntry and pollFirstEntry, holds one lock.
No. Wrap both in synchronized (map) { ... }.
Yes, but loops over them must still synchronize on the original wrapper.
Java 8.
java.util.concurrent.ConcurrentSkipListMap is a NavigableMap designed for concurrent use.

Next: synchronizedNavigableSet()

Wrap a NavigableSet so navigation and pollFirst() are thread-safe.

synchronizedNavigableSet() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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