Java TreeMap Methods

Beginner
6 min read
Updated: Nov 2026
45 method guides
Method index

About This Index

This page is the Java TreeMap Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data. All of them compile on Java 8 and later, except the putFirst() and putLast() guides, which need Java 21.

Learn what a TreeMap is on the TreeMap hub. Use this page when you need a specific method such as put(), floorEntry(), or subMap().

Always sorted, so output is predictable.

A TreeMap keeps its keys in ascending order (natural order or the order of the Comparator you pass in). Printing the map, looping over it, and every view follow that order, so the outputs shown in these guides are exactly what you will see on any JDK.

How to Call a TreeMap Method

Every method here is an instance method, so you call it on your map variable. Declare it as TreeMap or NavigableMap to reach the navigation methods:

Java
import java.util.TreeMap;

TreeMap<Integer, String> grades = new TreeMap<>();
grades.put(90, "A");                        // returns null (new key)
grades.put(80, "B");
grades.put(70, "C");
String g = grades.floorEntry(84).getValue(); // "B" (largest key <= 84)
Integer top = grades.lastKey();              // 90
System.out.println(grades.headMap(90));      // {70=C, 80=B}

Java TreeMap Methods Table

All method guides in this Java tutorial series (45 tutorials), grouped by purpose.

Add and update

11 methods

Store a value under a key, copy another map, add only when a key is free, replace values, or build the new value with a function. putFirst and putLast exist since Java 21 but always throw on a TreeMap.

MethodDescriptionTutorial
put()Add a mapping or overwrite the value of an existing key.Try it
putAll()Copy every mapping from another map into this one.Try it
putIfAbsent()Add a value only when the key is missing (or null).Try it
replace()Change the value of an existing key; never adds a key.Try it
replaceAll()Rewrite every value with a function of key and value.Try it
merge()Combine a new value with the old one, ideal for counters.Try it
compute()Calculate a new value from the key and its old value.Try it
computeIfAbsent()Create a value only when the key is missing.Try it
computeIfPresent()Update or drop a value only when the key exists.Try it
putFirst()Java 21 SequencedMap method that a TreeMap always rejects.Try it
putLast()Java 21 SequencedMap method that a TreeMap always rejects.Try it

Read and look up

7 methods

Look up a value by key, fall back to a default, test for keys or values, count the mappings, and see how the keys are ordered.

MethodDescriptionTutorial
get()Read the value stored under a key.Try it
getOrDefault()Read a value, or a fallback when the key is missing.Try it
containsKey()Check whether a key is present in O(log n) time.Try it
containsValue()Check whether any key maps to a value (a full scan).Try it
size()Count the key/value mappings.Try it
isEmpty()Check whether the map has no mappings.Try it
comparator()See which Comparator sorts the keys (null means natural order).Try it

Ranges and reversed views

6 methods

Live windows onto part of the map, or onto the whole map in reverse. Changes flow both ways between a view and its map.

MethodDescriptionTutorial
subMap()A live view of the keys between two bounds.Try it
headMap()A live view of the keys below a limit.Try it
tailMap()A live view of the keys from a starting key upward.Try it
descendingMap()A live reversed view of the whole map.Try it
descendingKeySet()Walk the keys from largest to smallest.Try it
navigableKeySet()A live NavigableSet of keys with floor, ceiling, and ranges.Try it

Views and loops

4 methods

Live views of the keys, the values, or the key/value pairs, plus a lambda loop. All of them follow ascending key order.

MethodDescriptionTutorial
keySet()A live, sorted Set view of all keys.Try it
values()A live Collection of the values, in key order.Try it
entrySet()Loop over key/value pairs in key order and edit values.Try it
forEach()Run a lambda for every key and value in sorted order.Try it

Remove and copy

5 methods

Delete one key, empty the map, take the smallest or largest entry out, or make a shallow copy.

MethodDescriptionTutorial
remove()Delete a key and get its old value back.Try it
clear()Remove every mapping and leave an empty map.Try it
pollFirstEntry()Remove and return the pair with the smallest key.Try it
pollLastEntry()Remove and return the pair with the largest key.Try it
clone()Make a shallow copy that keeps the same sort order.Try it

Frequently Asked Questions

They are the methods of java.util.TreeMap, a map that keeps its keys sorted. Besides the usual Map methods (put, get, remove, merge, keySet ...) it has navigation methods such as firstKey, floorEntry, and higherKey, and range views such as headMap, tailMap, and subMap.
This index lists 45 guides, from put() and get() to floorEntry(), subMap(), and pollFirstEntry(), grouped by what they do.
Both find the largest key that is less than or equal to the given value. floorKey returns only the key; floorEntry returns the key and its value together as a read-only snapshot. Both return null when nothing matches.
Java 6 added the NavigableMap methods: firstEntry, lastEntry, floorKey, ceilingKey, lowerKey, higherKey and their Entry versions, pollFirstEntry, pollLastEntry, descendingMap, descendingKeySet, navigableKeySet, and the boolean forms of headMap, tailMap, and subMap. Java 8 added getOrDefault, putIfAbsent, replace, replaceAll, forEach, merge, compute, computeIfAbsent, and computeIfPresent.
Read the Java TreeMap hub first. Then open put(), get(), floorEntry(), and headMap() from the tables below.

Start with put()

Store your first key/value pairs and watch the keys sort themselves.

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

12 people found this page helpful