Java TreeMap get() Method

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

Definition

In Java, get(key) returns the value that a TreeMap stores under key, or null if there is no such key. The map does not change.

The lookup walks down the red-black tree, comparing key with the keys it meets, so it takes O(log n) time. A key is found when the comparison returns 0; with a case-insensitive comparator, "FRANCE" finds the entry stored as "France".

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 key; you get the value or null.

Java
TreeMap<String, String> glossary = new TreeMap<>();
glossary.put("nibble", "4 bits");
String a = glossary.get("nibble");   // "4 bits"
String b = glossary.get("word");     // null
CallWhat it does
map.get(k)The value for k, or null.
map.getOrDefault(k, d)The value, or d when k is missing.
map.containsKey(k)Tells "missing" apart from "mapped to null".

The parameter type is Object, so get(42) on a TreeMap<String, ...> compiles, but it throws ClassCastException when the map compares 42 with its String keys.

Library declarations
Java
// java.util.TreeMap<K, V>
public V get(Object key)

// Declared in Map<K, V>

Parameters

ParameterDescription
key (Object)The key to look up. Not null under natural ordering.

Return Value

Return ValueDescription
VThe value mapped to a key that compares equal to key.
nullNo such key, or the key is mapped to null.

Compare with Similar Methods

Ways to read from a TreeMap:

MethodHow it differs
getOrDefault(k, d)Returns a fallback instead of null.
floorEntry(k)Finds the nearest key at or below k when there is no exact match.
containsKey(k)Only answers yes or no.
HashMap.get(k)Hash lookup, usually constant time; allows a null key.

Java Examples

Example 1 — Look Up Words in a Sorted Glossary

Read one definition, try a word that is missing, and react to the null.

Java
import java.util.TreeMap;

public class TreeMapGetGlossary {
    public static void main(String[] args) {
        TreeMap<String, String> glossary = new TreeMap<>();
        glossary.put("byte", "8 bits");
        glossary.put("bit", "a single 0 or 1");
        glossary.put("nibble", "4 bits");

        System.out.println("nibble = " + glossary.get("nibble"));
        System.out.println("word = " + glossary.get("word"));

        String meaning = glossary.get("kilobyte");
        if (meaning == null) {
            System.out.println("kilobyte is not in the glossary yet");
        }
    }
}

How It Works

  1. Exact hit. The search for "nibble" compares it with the keys on its path through the tree and stops where the comparison returns 0, giving "4 bits".
  2. A miss is null, not an error. "word" is not a key, so get returns null, and string concatenation prints the word null.
  3. Check before use. Testing meaning == null is the usual way to handle a missing entry when the map never stores null values.

Example 2 — Find Capitals Without Worrying About Case

The same data, once in a case-insensitive map and once in a naturally ordered copy.

Java
import java.util.TreeMap;

public class TreeMapGetIgnoreCase {
    public static void main(String[] args) {
        TreeMap<String, String> capitals = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
        capitals.put("France", "Paris");
        capitals.put("Japan", "Tokyo");

        TreeMap<String, String> strict = new TreeMap<>();
        strict.putAll(capitals);

        System.out.println("FRANCE (ignore case): " + capitals.get("FRANCE"));
        System.out.println("japan (ignore case): " + capitals.get("japan"));
        System.out.println("FRANCE (natural order): " + strict.get("FRANCE"));
    }
}

How It Works

  1. Lookup uses the comparator. In capitals, "FRANCE" compares equal to "France", so get returns Paris even though "FRANCE".equals("France") is false.
  2. Any spelling works. "japan" in lowercase finds Japan for the same reason.
  3. Natural order is strict. strict was created without a comparator, so "FRANCE" and "France" are different keys there and the lookup returns null.

Example 3 — Missing Key, null Value, and null Key

Locker B07 has no code yet (null), C99 does not exist, and get(null) is not allowed.

Java
import java.util.TreeMap;

public class TreeMapGetNullCases {
    public static void main(String[] args) {
        TreeMap<String, Integer> lockerCodes = new TreeMap<>();
        lockerCodes.put("A12", 4071);
        lockerCodes.put("B07", null);

        System.out.println("B07: " + lockerCodes.get("B07") + ", has key: " + lockerCodes.containsKey("B07"));
        System.out.println("C99: " + lockerCodes.get("C99") + ", has key: " + lockerCodes.containsKey("C99"));
        try {
            lockerCodes.get(null);
        } catch (NullPointerException e) {
            System.out.println("get(null): " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Same result, different meaning. Both B07 and C99 give null from get; only containsKey reveals that B07 exists with a null value.
  2. The key itself cannot be null. With natural ordering the map cannot compare null, so get(null) throws instead of returning null.
  3. Unboxing trap avoided. The results are printed as Integer; writing int code = lockerCodes.get("B07"); would throw NullPointerException.

Frequently Asked Questions

null. The map is not changed and no exception is thrown (unless the key itself is null or of the wrong type).
O(log n). The tree is searched from the root, comparing keys on the way down.
TreeMap matches keys with its comparator, not with equals. A case-insensitive comparator makes "FRANCE" and "France" the same key.
Not with natural ordering: it throws NullPointerException. Only a comparator that accepts null allows it.
Call containsKey(key). It returns true for a key mapped to null and false for a missing key.

Next: getOrDefault()

Read a value, or a fallback when the key is missing.

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

7 people found this page helpful