Java Collections unmodifiableNavigableMap() Method

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

Definition

In Java 8 and later, Collections.unmodifiableNavigableMap(m) returns a read-only NavigableMap view. Lookups such as floorEntry, ceilingKey, and higherEntry work, as do descending and inclusive range views.

Entries handed back by these lookups are read-only too, and pollFirstEntry()/pollLastEntry() throw UnsupportedOperationException because they remove what they return.

A static method of the java.util.Collections utility class, available since Java 8. Call it on the class name, as in Collections.unmodifiableNavigableMap(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Wrap a TreeMap or another NavigableMap.

Java
NavigableMap<Integer, Integer> rates = Collections.unmodifiableNavigableMap(table);
int price = rates.floorEntry(weight).getValue();   // band for this weight
CallWhat it does
floorEntry(k) / ceilingEntry(k)Closest entry at or below / above k.
floorKey(k) / ceilingKey(k)Just the key of that entry.
descendingMap()Read-only reverse-order view.
pollFirstEntry() / pollLastEntry()Throw UnsupportedOperationException.

Added in Java 8. The NavigableMap interface exists since Java 6; earlier versions can only wrap it as unmodifiableSortedMap.

Library declarations
Java
// java.util.Collections (Java 8+)
public static <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K, ? extends V> m)

Parameters

ParameterDescription
mThe navigable map to expose read-only. Must not be null.

Return Value

Return ValueDescription
NavigableMap<K,V>A read-only view of m.

Compare with Similar Methods

Related wrappers:

MethodHow it differs
unmodifiableSortedMap(m)Older; only firstKey/lastKey and half-open ranges.
synchronizedNavigableMap(m)Thread-safe, still changeable.
emptyNavigableMap()Read-only navigable map with no entries.

Java Examples

Example 1 — Shipping Price by Weight Band

Each key is the lowest weight of a band. floorEntry finds the band a parcel belongs to.

Java
import java.util.Collections;
import java.util.NavigableMap;
import java.util.TreeMap;

public class CollectionsUnmodifiableNavigableMapShip {
    public static void main(String[] args) {
        NavigableMap<Integer, Integer> table = new TreeMap<>();
        table.put(0, 5);
        table.put(2, 9);
        table.put(10, 20);
        NavigableMap<Integer, Integer> rates = Collections.unmodifiableNavigableMap(table);

        int[] weights = {1, 2, 7, 25};
        for (int kg : weights) {
            System.out.println(kg + " kg -> $" + rates.floorEntry(kg).getValue());
        }
    }
}

How It Works

  1. Bands as keys. 0, 2, and 10 kg start the three price bands of $5, $9, and $20.
  2. Round down to a band. floorEntry(7) finds the greatest key not above 7, which is 2, so the price is $9.
  3. Exact and large weights. 2 kg matches its key exactly, and 25 kg falls into the open-ended band starting at 10.

Example 2 — Returned Entries and Polls Are Read-Only

Get an entry from the view and try to edit it, then try to poll the last band.

Java
import java.util.Collections;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

public class CollectionsUnmodifiableNavigableMapEntries {
    public static void main(String[] args) {
        NavigableMap<Integer, Integer> table = new TreeMap<>();
        table.put(0, 5);
        table.put(2, 9);
        table.put(10, 20);
        NavigableMap<Integer, Integer> rates = Collections.unmodifiableNavigableMap(table);

        Map.Entry<Integer, Integer> band = rates.floorEntry(7);
        System.out.println("floorEntry(7) = " + band);
        try {
            band.setValue(1);
        } catch (UnsupportedOperationException e) {
            System.out.println("setValue: " + e.getClass().getSimpleName());
        }
        try {
            rates.pollLastEntry();
        } catch (UnsupportedOperationException e) {
            System.out.println("pollLastEntry: " + e.getClass().getSimpleName());
        }
        System.out.println("table unchanged: " + table);
    }
}

How It Works

  1. Entry printed. The band for 7 kg prints as 2=9, key and value joined by "=".
  2. Wrapped entry. The view returns a read-only entry, so setValue(1) cannot cut the price.
  3. Poll blocked. pollLastEntry() would remove the 10 kg band; it throws, and the underlying table still has three bands.

Example 3 — Age Groups: Reverse, Inclusive, and Key Views

Explore a read-only map of age groups with descending, inclusive, and key-set views.

Java
import java.util.Collections;
import java.util.NavigableMap;
import java.util.TreeMap;

public class CollectionsUnmodifiableNavigableMapViews {
    public static void main(String[] args) {
        NavigableMap<Integer, String> groups = new TreeMap<>();
        groups.put(0, "child");
        groups.put(13, "teen");
        groups.put(20, "adult");
        groups.put(65, "senior");
        NavigableMap<Integer, String> view = Collections.unmodifiableNavigableMap(groups);

        System.out.println("descending: " + view.descendingMap());
        System.out.println("up to 13 inclusive: " + view.headMap(13, true));
        System.out.println("keys: " + view.navigableKeySet() + ", ceilingKey(30) = " + view.ceilingKey(30));
        try {
            view.headMap(13, true).put(5, "kid");
        } catch (UnsupportedOperationException e) {
            System.out.println("put via headMap: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Reverse order. descendingMap() lists the groups from oldest to youngest.
  2. Inclusive bound. headMap(13, true) keeps key 13 itself, which the one-argument headMap(13) would drop.
  3. Keys and lookups. navigableKeySet() shows the boundaries, ceilingKey(30) jumps to 65, and inserting through the head view throws.

Frequently Asked Questions

It returns a read-only NavigableMap view that supports floorEntry, ceilingKey, descendingMap, and other navigation methods.
No. The entry is read-only and setValue throws UnsupportedOperationException.
It removes the entry it returns. Use firstEntry() to read without removing.
Java 8.
Yes. Every sub-map and key view is wrapped as read-only.

Next: unmodifiableNavigableSet()

Wrap a NavigableSet in a read-only view that still supports ceiling and floor.

unmodifiableNavigableSet() 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.

8 people found this page helpful