Home Java TreeMap Methods tailMap() Java TreeMap tailMap() Method 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.
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 Call What 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.util.TreeMap<K, V>
public SortedMap<K, V> tailMap(K fromKey)
public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) // Java 6 Parameters Parameter Description fromKey (K)Lower bound of the view. inclusive (boolean)Two-argument form only: false to exclude fromKey.
Return Value Return Value Description 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:
Java Examples 1. From today on 2. Threshold filter 3. Live growth Example 1 — Upcoming Events, With and Without Today Dates are ISO strings; one call includes today's event, the other skips it.
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));
}
} from today: {2026-11-19=Dentist, 2026-11-21=Party}
after today: {2026-11-21=Party}
already past: {2026-11-17=Gym} How It Works Today counts by default. The one-argument tailMap is inclusive, so the dentist appointment on 2026-11-19 is listed.Strictly in the future. tailMap(today, false) drops today's key and keeps only the party.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.
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());
}
} honours: [Jun, Kim, Max]
count: 3
lowest honours score: 90 How It Works The threshold is in. Jun scored exactly 90, and the inclusive bound puts him on the list; Lou's 88 falls just short.Names from the view. values() on the tail returns the students in score order, lowest qualifying score first.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.
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());
}
}
} view: {105=Bea}
view after two puts: {105=Bea, 110=Cal}
map: {101=Ali, 102=Zoe, 104=Dan, 105=Bea, 110=Cal}
fromNow.put(100): IllegalArgumentException How It Works 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.Adding through the view. Ticket 104 is inside the range, so fromNow.put stores it in tickets in its sorted place.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 Is the key passed to TreeMap tailMap() included? Yes, tailMap(k) is inclusive. Use tailMap(k, false) to start strictly after k.
Does tailMap() return a copy? No, a live view. New keys at or above the bound appear in it, and changes through it reach the map.
Why does tailMap() view throw IllegalArgumentException on put()? The key you are adding is below the view's lower bound.
How do I split a TreeMap at a key? headMap(k) gives everything below k and tailMap(k) everything from k on; the two never overlap.
What does tailMap() return? tailMap(k) returns SortedMap; tailMap(k, inclusive) returns NavigableMap, which supports navigation methods such as floorKey and pollFirstEntry.
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.
14 people found this page helpful
Helpful Share Copy link Suggestion