In Java, firstKey() returns the lowest key currently in a TreeMap: the key that comes first in the map's sort order. It only reads the map; nothing is removed.
"Lowest" is decided by the map's ordering. With natural ordering it is the smallest number or the alphabetically first string; with a reverse comparator it is the largest value. The method comes from SortedMap and runs in O(log n) time, following left children down from the root.
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
No arguments; you get the key itself, not the value.
Java
TreeMap<String, String> slots = new TreeMap<>();
slots.put("14:00", "Dentist");
slots.put("09:30", "Stand-up");
String first = slots.firstKey(); // "09:30"
Call
What it does
map.firstKey()
The lowest key; throws on an empty map.
map.firstEntry()
Key and value together, or null when empty.
map.get(map.firstKey())
The value that belongs to the lowest key.
The return type is K, so for a TreeMap<Integer, ...> you can write int low = map.firstKey();. That unboxing is safe here because firstKey never returns null for a natural-order map; it throws instead.
Library declarations
Java
// java.util.TreeMap<K, V>
public K firstKey()
// Declared in SortedMap<K, V>
Parameters
Parameter
Description
(none)
firstKey takes no parameters.
Return Value
Return Value
Description
K
The first (lowest) key according to the map's ordering.
Times are zero-padded strings, so the alphabetically first key is also the earliest time.
Java
import java.util.TreeMap;
public class TreeMapFirstKeyAppointments {
public static void main(String[] args) {
TreeMap<String, String> appointments = new TreeMap<>();
appointments.put("14:00", "Dentist");
appointments.put("09:30", "Team stand-up");
appointments.put("11:15", "Lunch with Ana");
String first = appointments.firstKey();
System.out.println("first slot: " + first + " (" + appointments.get(first) + ")");
appointments.remove(first);
System.out.println("after it is done: " + appointments.firstKey());
}
}
Output
first slot: 09:30 (Team stand-up)
after it is done: 11:15
How It Works
Insertion order is irrelevant. "14:00" went in first, yet firstKey() answers "09:30", because the tree keeps the string keys sorted no matter when they arrived.
Key first, value second.firstKey hands back only the time; a follow-up get(first) fetches the description "Team stand-up".
The front moves on. Once the 09:30 slot is removed, the next call reports "11:15", the earliest of the two appointments still booked.
Example 2 — firstKey() Throws Where firstEntry() Returns null
An empty ticket queue shows the two ways the "lowest" methods report that there is nothing to return.
Oldest year under natural order.Integer keys sort numerically, so the ascending map starts at 2019.
Newest year once reversed.Collections.reverseOrder() puts 2024 at the front of descending, and firstKey() follows the comparator, not the size of the number.
Printing confirms it. The reversed map lists 2024, 2021, 2019; firstKey() is always whatever key would be printed first.
Frequently Asked Questions
The lowest key according to the map's ordering: the smallest number or alphabetically first string under natural ordering, or the first key the comparator puts at the front.
It throws NoSuchElementException. Use firstEntry(), which returns null, or check isEmpty() before calling it.
No. It only reads the map. To read and remove the lowest entry in one step, use pollFirstEntry().
firstKey() returns only the key and throws on an empty map. firstEntry() returns an immutable key/value snapshot and returns null on an empty map.
O(log n). It walks down the left side of the red-black tree from the root to the leftmost node.