Java TreeMap tailMap() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.TreeMap

Definition

In Java, tailMap(fromKey) returns the part of a TreeMap whose keys are greater than or equal to fromKey: the bound is inclusive. The Java 6 overload tailMap(fromKey, inclusive) lets you exclude it.

The view is live: it grows when matching keys are added to the map and writes through when you change it. The one-argument form returns SortedMap, the two-argument form NavigableMap.

An instance method of java.util.TreeMap, part of TreeMap since Java 1.2. A TreeMap keeps its key/value pairs sorted by key in a red-black tree, so lookups and updates take O(log n) time; it is not thread-safe.

Syntax

Pass the lower bound; add false to leave it out.

Java
TreeMap<String, String> agenda = new TreeMap<>();
SortedMap<String, String> fromToday = agenda.tailMap("2026-11-19");            // keys >= today
NavigableMap<String, String> afterToday = agenda.tailMap("2026-11-19", false); // keys > today
CallWhat it does
map.tailMap(k)Keys ≥ k (inclusive).
map.tailMap(k, false)Keys > k (exclusive).
map.tailMap(k).size()How many keys are at or above k.

headMap is exclusive and tailMap is inclusive by default, so headMap(k) and tailMap(k) split a map into two parts with no overlap and no gap.

Library declarations
Java
// java.util.TreeMap<K, V>
public SortedMap<K, V> tailMap(K fromKey)

public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive)   // Java 6

Parameters

ParameterDescription
fromKey (K)Lower bound of the view.
inclusive (boolean)Two-argument form only: false to exclude fromKey.

Return Value

Return ValueDescription
SortedMap<K, V>One-argument form: live view of keys ≥ fromKey.
NavigableMap<K, V>Two-argument form: live view with navigation methods.

Compare with Similar Methods

Choosing a range view:

MethodHow it differs
headMap(k)Keys below k, exclusive.
subMap(a, b)Keys between two bounds.
ceilingKey(k)Only the first key of the tail.
descendingMap()Reverse the order of a map or view.

Java Examples

Example 1 — Upcoming Events, With and Without Today

Dates are ISO strings; one call includes today's event, the other skips it.

Java
import java.util.TreeMap;

public class TreeMapTailMapAgenda {
    public static void main(String[] args) {
        TreeMap<String, String> agenda = new TreeMap<>();
        agenda.put("2026-11-17", "Gym");
        agenda.put("2026-11-19", "Dentist");
        agenda.put("2026-11-21", "Party");

        String today = "2026-11-19";
        System.out.println("from today: " + agenda.tailMap(today));
        System.out.println("after today: " + agenda.tailMap(today, false));
        System.out.println("already past: " + agenda.headMap(today));
    }
}

How It Works

  1. Today counts by default. The one-argument tailMap is inclusive, so the dentist appointment on 2026-11-19 is listed.
  2. Strictly in the future. tailMap(today, false) drops today's key and keeps only the party.
  3. A clean split. headMap(today) returns the past event; together with the inclusive tail it covers every date exactly once.

Example 2 — Students Scoring 90 or More

Scores are keys, so the honours list is the tail from 90.

Java
import java.util.SortedMap;
import java.util.TreeMap;

public class TreeMapTailMapHonours {
    public static void main(String[] args) {
        TreeMap<Integer, String> scores = new TreeMap<>();
        scores.put(72, "Ivo");
        scores.put(90, "Jun");
        scores.put(95, "Kim");
        scores.put(88, "Lou");
        scores.put(99, "Max");

        SortedMap<Integer, String> honours = scores.tailMap(90);
        System.out.println("honours: " + honours.values());
        System.out.println("count: " + honours.size());
        System.out.println("lowest honours score: " + honours.firstKey());
    }
}

How It Works

  1. The threshold is in. Jun scored exactly 90, and the inclusive bound puts him on the list; Lou's 88 falls just short.
  2. Names from the view. values() on the tail returns the students in score order, lowest qualifying score first.
  3. Size and first key of the range. The view knows it holds 3 entries and that its smallest key is 90, without touching the scores below.

Example 3 — A Tail View Grows With the Map

Ticket numbers from 103 on are watched through a view; the view also accepts in-range additions.

Java
import java.util.SortedMap;
import java.util.TreeMap;

public class TreeMapTailMapGrowing {
    public static void main(String[] args) {
        TreeMap<Integer, String> tickets = new TreeMap<>();
        tickets.put(101, "Ali");
        tickets.put(105, "Bea");

        SortedMap<Integer, String> fromNow = tickets.tailMap(103);
        System.out.println("view: " + fromNow);
        tickets.put(110, "Cal");
        tickets.put(102, "Zoe");
        System.out.println("view after two puts: " + fromNow);

        fromNow.put(104, "Dan");
        System.out.println("map: " + tickets);
        try {
            fromNow.put(100, "Eli");
        } catch (IllegalArgumentException e) {
            System.out.println("fromNow.put(100): " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Only matching keys appear. Of the two new tickets, 110 is at least 103 and shows up in the view, while 102 stays visible only in the map.
  2. Adding through the view. Ticket 104 is inside the range, so fromNow.put stores it in tickets in its sorted place.
  3. Below the bound is refused. 100 is under 103; the view throws IllegalArgumentException rather than silently storing a key it could not show.

Frequently Asked Questions

Yes, tailMap(k) is inclusive. Use tailMap(k, false) to start strictly after k.
No, a live view. New keys at or above the bound appear in it, and changes through it reach the map.
The key you are adding is below the view's lower bound.
headMap(k) gives everything below k and tailMap(k) everything from k on; the two never overlap.
tailMap(k) returns SortedMap; tailMap(k, inclusive) returns NavigableMap, which supports navigation methods such as floorKey and pollFirstEntry.

Next: values()

A live Collection of the values, in key order.

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

14 people found this page helpful