Java Collections synchronizedNavigableSet() Method

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

Definition

In Java 8 and later, Collections.synchronizedNavigableSet(s) returns a thread-safe NavigableSet wrapper. Single calls such as ceiling, floor, pollFirst, and pollLast each run under one lock.

pollFirst() reads and removes in one call, so it is a ready-made atomic "take the next one". Combinations like ceiling followed by remove still need synchronized (set) { ... }.

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

Syntax

Wrap a new TreeSet.

Java
NavigableSet<Integer> free = Collections.synchronizedNavigableSet(new TreeSet<Integer>());
Integer seat = free.pollFirst();              // atomic take
synchronized (free) {                         // compound: find then remove
    Integer s = free.ceiling(4);
    if (s != null) free.remove(s);
}
CallWhat it does
pollFirst() / pollLast()Atomic read-and-remove.
ceiling / floor / higher / lowerAtomic single lookups.
descendingSet() / headSet(x, incl)Views guarded by the same lock.
synchronized (set) { ... }Needed for loops and multi-step logic.

Added in Java 8. On Java 6 or 7 you can wrap a TreeSet with synchronizedSortedSet, but you lose the navigation methods on the wrapper type.

Library declarations
Java
// java.util.Collections (Java 8+)
public static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s)

Parameters

ParameterDescription
sThe navigable set to make thread-safe. Do not use it directly afterwards.

Return Value

Return ValueDescription
NavigableSet<T>A synchronized view of s.

Compare with Similar Methods

Related wrappers:

MethodHow it differs
synchronizedSortedSet(s)Older; no pollFirst or ceiling.
unmodifiableNavigableSet(s)Read-only navigable view.
checkedNavigableSet(s, type)Type-checked navigable set.

Java Examples

Example 1 — Two Agents Booking Seats with pollFirst

Agents keep taking the lowest free seat until none remain.

Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;

public class CollectionsSynchronizedNavigableSetSeats {
    public static void main(String[] args) throws InterruptedException {
        NavigableSet<Integer> free = Collections.synchronizedNavigableSet(new TreeSet<Integer>());
        for (int s = 1; s <= 10; s++) {
            free.add(s);
        }
        List<Integer> booked = Collections.synchronizedList(new ArrayList<Integer>());
        Runnable agent = () -> {
            Integer seat;
            while ((seat = free.pollFirst()) != null) {
                booked.add(seat);
            }
        };
        Thread a = new Thread(agent);
        Thread b = new Thread(agent);
        a.start();
        b.start();
        a.join();
        b.join();
        System.out.println("booked " + booked.size() + ", distinct " + new TreeSet<>(booked).size() + ", free " + free.size());
    }
}

How It Works

  1. One call does both. pollFirst() finds the lowest seat and removes it under the lock, so no extra block is needed.
  2. No double booking. Each seat goes to exactly one agent: 10 bookings, 10 distinct seats.
  3. Stop on null. When the set is empty, pollFirst() returns null and each agent's loop ends.

Example 2 — Book the Nearest Seat at or Above a Request

Finding a seat with ceiling and removing it are two calls, so they share one lock.

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class CollectionsSynchronizedNavigableSetNearest {
    static Integer book(NavigableSet<Integer> free, int wanted) {
        synchronized (free) {
            Integer seat = free.ceiling(wanted);
            if (seat != null) {
                free.remove(seat);
            }
            return seat;
        }
    }

    public static void main(String[] args) {
        NavigableSet<Integer> free = Collections.synchronizedNavigableSet(new TreeSet<>(Arrays.asList(2, 4, 5, 9)));
        System.out.println("wanted 4 -> " + book(free, 4));
        System.out.println("wanted 4 -> " + book(free, 4));
        System.out.println("wanted 10 -> " + book(free, 10));
        System.out.println("left: " + free);
    }
}

How It Works

  1. Exact hit. Seat 4 is free, so ceiling(4) returns it and it is removed.
  2. Next best. The second request for 4 finds it gone; ceiling moves up to 5.
  3. Nothing high enough. No seat is 10 or above, so the result is null. Holding the lock ensures no other thread takes the seat between lookup and removal.

Example 3 — Expire Old Timestamps, Read Newest First

Clear a head view to drop old entries, then iterate the descending view under the lock.

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class CollectionsSynchronizedNavigableSetExpire {
    public static void main(String[] args) {
        NavigableSet<Integer> times = Collections.synchronizedNavigableSet(
            new TreeSet<>(Arrays.asList(100, 140, 205, 260, 300)));

        times.headSet(200, false).clear();

        StringBuilder sb = new StringBuilder();
        synchronized (times) {
            for (int t : times.descendingSet()) {
                sb.append(t).append(' ');
            }
        }
        System.out.println("newest first: " + sb.toString().trim());
        System.out.println("lower(260) = " + times.lower(260));
    }
}

How It Works

  1. Bulk expiry. headSet(200, false) is a view of times below 200; clearing it deletes 100 and 140 from the real set.
  2. Descending loop. The reverse view still needs synchronized (times) while iterating, locking the original wrapper.
  3. Navigation. lower(260) returns the closest time strictly below 260, which is 205.

Frequently Asked Questions

It returns a thread-safe NavigableSet wrapper; each call, including ceiling and pollFirst, holds one lock.
Yes. It reads and removes the lowest element in a single locked call.
No. Put both calls inside synchronized (set) { ... }.
Java 8.
Yes. Synchronize on the original wrapper for the whole loop.

Next: synchronizedSet()

Wrap a Set so each method call is thread-safe.

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

10 people found this page helpful