Java TreeMap Methods

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().
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:
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 methodsStore 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.
| Method | Description | Tutorial |
|---|---|---|
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 methodsLook 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.
| Method | Description | Tutorial |
|---|---|---|
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 methodsLive windows onto part of the map, or onto the whole map in reverse. Changes flow both ways between a view and its map.
| Method | Description | Tutorial |
|---|---|---|
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 methodsLive views of the keys, the values, or the key/value pairs, plus a lambda loop. All of them follow ascending key order.
Remove and copy
5 methodsDelete one key, empty the map, take the smallest or largest entry out, or make a shallow copy.
| Method | Description | Tutorial |
|---|---|---|
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
Start with put()
Store your first key/value pairs and watch the keys sort themselves.
12 people found this page helpful
