TreeMap

Su Suriya Ravichandran Updated 14 Nov 2026
5 min read ·Lesson 76 of 306

A TreeMap is a map that keeps its entries sorted by key. It supports everything a HashMap does (put, get, remove, iterate) but always iterates in key order and adds methods that depend on that order: the smallest or largest key, the nearest key to a given value, and views of key ranges. It is implemented as a red-black tree, so its main operations take O(log n) time.

Creating and Using a TreeMap

import java.util.Map;
import java.util.TreeMap;

TreeMap<String, Integer> branches = new TreeMap<>();
branches.put("mumbai", 3);
branches.put("chennai", 5);
branches.put("pune", 1);
branches.put("delhi", 2);

System.out.println(branches);   // {chennai=5, delhi=2, mumbai=3, pune=1}

The keys come out in alphabetical order regardless of insertion order. The usual map methods (get, put, remove, containsKey, getOrDefault, merge, computeIfAbsent, and so on) behave exactly as described in the HashMap lesson.

Key Ordering

Keys are ordered by their natural ordering (they must implement Comparable) or by a Comparator passed to the constructor:

TreeMap<String, Integer> caseInsensitive = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
caseInsensitive.put("Java", 1);
caseInsensitive.put("java", 2);          // same key under this comparator: replaces the value
System.out.println(caseInsensitive);     // {Java=2}

TreeMap<Integer, String> descending = new TreeMap<>(Comparator.reverseOrder());
descending.putAll(Map.of(1, "one", 3, "three", 2, "two"));
System.out.println(descending);          // {3=three, 2=two, 1=one}

As with TreeSet, keys are considered equal when the comparison returns 0. equals and hashCode are not used. In the first example, "Java" and "java" count as the same key, so the second put replaces the value but keeps the original key.

Keys without a natural ordering and no comparator cause ClassCastException on the first put. A null key is not allowed with natural ordering; null values are allowed.

TreeMap implements NavigableMap. Using the map above, {chennai=5, delhi=2, mumbai=3, pune=1}:

MethodReturnsExample result
firstKey() / lastKey()Smallest / largest keychennai / pune
firstEntry() / lastEntry()Smallest / largest entrychennai=5 / pune=1
floorKey(k)Greatest key ≤ kfloorKey("e") → delhi
ceilingKey(k)Smallest key ≥ kceilingKey("e") → mumbai
lowerKey(k) / higherKey(k)Strictly less / strictly greaterhigherKey("delhi") → mumbai
floorEntry(k), ceilingEntry(k), ...The same, returning the whole entry
pollFirstEntry() / pollLastEntry()Remove and return the smallest / largest entry

The navigation methods return null when no such key exists. firstKey and lastKey throw NoSuchElementException on an empty map, while firstEntry and lastEntry return null.

floorEntry is the key to many real problems: "which price band does this amount fall into?", "which configuration version was active at this time?", "which tax slab applies to this income?". The answer is the entry with the greatest key not exceeding the value.

Range Views

branches.headMap("mumbai");           // {chennai=5, delhi=2}          keys < "mumbai"
branches.tailMap("mumbai");           // {mumbai=3, pune=1}            keys ≥ "mumbai"
branches.subMap("d", "p");            // {delhi=2, mumbai=3}           "d" ≤ key < "p"
branches.headMap("mumbai", true);     // {chennai=5, delhi=2, mumbai=3}
branches.descendingMap();             // {pune=1, mumbai=3, delhi=2, chennai=5}
branches.navigableKeySet();           // sorted view of the keys

The same conventions as TreeSet apply: the lower bound is inclusive and the upper bound exclusive by default, and overloads with boolean flags control each end. These are live views: they reflect later changes to the map, and modifications through a view update the map.

Iterating

Iteration over keySet(), values(), entrySet(), or forEach always follows key order:

TreeMap<LocalDate, Double> dailySales = new TreeMap<>();
dailySales.put(LocalDate.of(2026, 11, 3), 18_400.0);
dailySales.put(LocalDate.of(2026, 11, 1), 12_250.0);
dailySales.put(LocalDate.of(2026, 11, 2), 9_800.0);

dailySales.forEach((date, amount) -> System.out.println(date + "  " + amount));

Output:

2026-11-01  12250.0
2026-11-02  9800.0
2026-11-03  18400.0

LocalDate keys sort chronologically, which makes TreeMap a natural fit for time-series data: daily totals, price histories, event logs.

Performance

OperationTreeMapHashMap
get, put, remove, containsKeyO(log n)O(1) average
Iterate in key orderO(n)Requires sorting: O(n log n)
firstKey, floorKey, ceilingKey, rangesO(log n)Not available

Use TreeMap when you need sorted keys or nearest-key lookups. When you only need fast lookups by exact key, HashMap is faster.

Common Uses

Counting with sorted output

TreeMap<String, Integer> wordCount = new TreeMap<>();
for (String w : "the cat and the hat and the bat".split(" ")) {
    wordCount.merge(w, 1, Integer::sum);
}
System.out.println(wordCount);   // {and=2, bat=1, cat=1, hat=1, the=3}

The result is alphabetized automatically, with no separate sorting step.

Leaderboards and top-N

With a reverse-order comparator, the highest keys come first, and pollFirstEntry removes the current leader:

TreeMap<Integer, String> board = new TreeMap<>(Comparator.reverseOrder());
board.put(820, "ada");
board.put(910, "linus");
board.put(760, "grace");
System.out.println(board.firstEntry());   // 910=linus

Note that each score can appear only once as a key. When several players can share a score, use TreeMap<Integer, List<String>> with computeIfAbsent.

Range-based lookups

The practical example below uses floorEntry, headMap, and higherKey to compute a slab-based electricity bill.

TreeMap Versus HashMap Versus LinkedHashMap

NeedChoose
Fastest lookups, order irrelevantHashMap
Remember insertion orderLinkedHashMap
Keys always sorted; nearest-key and range queriesTreeMap
Thread-safe sorted mapConcurrentSkipListMap

Practical Example: Slab-Based Electricity Tariff

A utility charges per unit at increasing rates as consumption crosses slab boundaries. Each slab starts at a number of units, and the rate applies to units from that point up to the next slab's start. (The rates below are illustrative.)

import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

public class Tariff {
    // slab start (units) -> rate per unit for units from that start upward
    static final NavigableMap<Integer, Double> RATES = new TreeMap<>(Map.of(
        0, 0.0,
        100, 2.35,
        200, 4.70,
        400, 6.30,
        500, 8.40
    ));

    static double bill(int units) {
        double total = 0;
        for (Map.Entry<Integer, Double> slab : RATES.headMap(units, false).entrySet()) {
            int start = slab.getKey();
            Integer nextStart = RATES.higherKey(start);
            int end = (nextStart == null) ? units : Math.min(units, nextStart);
            total += (end - start) * slab.getValue();
        }
        return total;
    }

    public static void main(String[] args) {
        for (int units : new int[]{80, 150, 450, 620}) {
            double rate = RATES.floorEntry(units).getValue();
            System.out.printf("%4d units: bill %8.2f, marginal rate %.2f%n", units, bill(units), rate);
        }
        System.out.println("Slabs above 200: " + RATES.tailMap(200, false));
    }
}

Output:

  80 units: bill     0.00, marginal rate 0.00
 150 units: bill   117.50, marginal rate 2.35
 450 units: bill  1490.00, marginal rate 6.30
 620 units: bill  2813.00, marginal rate 8.40
Slabs above 200: {400=6.3, 500=8.4}

The TreeMap holds slab start points in order. floorEntry(units) finds the slab containing the last unit consumed, giving the marginal rate in a single O(log n) call. bill iterates only over the slabs that begin below the consumption (headMap(units, false)), and higherKey finds where each slab ends. For 450 units: 100 units at 2.35, 200 units at 4.70, and 50 units at 6.30, totalling 1,490. Map.of provides the data unordered; wrapping it in a TreeMap sorts the keys, so adding a new slab requires no other code changes.

0 Comments

Reviewed before they appear

No comments yet.

Java
Java Introduction Java Features Java Syntax Java Comments Java Statements Java Output Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Numbers Java Math Java Booleans Java User Input Java Conditions Java If Else Java Switch Java While Loop Java For Loop Java Break Java Continue Java Arrays Java Methods Java Parameters Java Return Values Java Scope Java Recursion Install Java Install JDK Java IDEs Java JDK Java JRE Java JVM Java Compiler Java File Compile Java Run Java JShell Basics Java Packages Java OOP Java Classes Java Objects Class Methods Constructors Constructor Overload this Keyword static Keyword final Keyword Encapsulation Inheritance Polymorphism Abstraction Access Modifiers super Keyword Method Overload Method Override Instanceof Operator Nested Classes Inner Classes Anonymous Classes Enums Records Array Basics Array Length Loop Arrays Multidimensional Arrays Array Copy Array Sorting Array Search ArrayList LinkedList HashSet TreeSet HashMap TreeMap Queue Interface Deque Interface Stack Class Iterator List Interface Set Interface Map Interface Collections Class Comparable Comparator Generics Basics Generic Classes Generic Methods Wildcards String Basics String Length String Methods String Concatenation String Compare String Search String Replace String Split String Format StringBuilder StringBuffer Text Blocks Regular Expressions Number Types Integer Class Float Class Double Class BigInteger BigDecimal Math Class Random Numbers Number Format Date Basics LocalDate LocalTime LocalDateTime ZonedDateTime Date Formatting Date Parsing Period Class Duration Class Java Instant Class Java Exception Basics Java try...catch Java Multiple Catch Blocks Java finally Block Java throw Keyword Java throws Keyword Java Checked Exceptions Java Unchecked Exceptions Java Custom Exceptions Java try-with-resources Java Exception Chaining Java File Basics Java Create Files Java Read Files Java Write Files Java Delete Files Java File Paths Java File Class Java BufferedReader Java BufferedWriter Java FileInputStream Java FileOutputStream Java Object Streams Java Directory Handling Java NIO Basics Java Files API Java Input Java Scanner Class Java Console Input Java PrintStream Java InputStream Java OutputStream Java Reader Class Java Writer Class Java Buffered Streams Java Data Streams Java Generics Introduction Java Generic Types Java Generic Methods Java Bounded Types Java Wildcards Java Type Erasure Java Generic Interfaces Java Generic Collections Java Lambda Basics Java Lambda Syntax Java Functional Interfaces Java Method References Java Stream API Java Stream Creation Java Stream Filter Java Stream Map Java Stream Reduce Java Stream Sort Java Stream Collect Java Collectors Class Java Optional Class Java Parallel Streams Java Threads: Thread Basics Create a Thread in Java Java Thread Class Java Runnable Interface Java Thread States Java Thread Sleep Java Thread Join Java Thread Priority Java Synchronization Java Thread Safety Java Race Conditions Java Deadlocks Java Executor Service Java Thread Pools Java Callable Interface Java Future Interface Java CompletableFuture Java Concurrent Collections Java Atomic Classes Java Virtual Threads Java Networking Basics Java URL Class Java URI Class Java HttpClient Java HTTP Requests Java HTTP Responses Java Socket Basics Java ServerSocket Java DatagramSocket Java WebSocket Basics Java JDBC Basics Java JDBC Setup Java Database Connection Java JDBC Connection Interface Java JDBC Statement Java PreparedStatement Java ResultSet Java JDBC CRUD Operations Java JDBC Transactions Java JDBC Batch Update Java Connection Pooling with HikariCP Java JPA Basics: Entities, EntityManager and JPQL Java Hibernate Basics: SessionFactory, Session and HQL Package in Java: Basics Explained with Examples Create Package in Java: Step-by-Step Guide with Examples Import Package in Java: The import Statement Explained Access Control in Java: Packages, Classes and Members JAR File in Java: Create, Run and Inspect JAR Files Java Modules: Introduction to the Java Module System module-info in Java: Every Module Directive Explained Java Module Path: How --module-path Finds Your Modules Java Classpath: How -cp and CLASSPATH Find Your Classes Java Annotations: Basics of @ Metadata in Java Java Built-in Annotations: @Override, @Deprecated and More Java Custom Annotations: Create Your Own @interface Java Annotation Targets: @Target and ElementType Explained Java Reflection API: Inspect and Use Classes at Runtime Java Class Metadata: Names, Modifiers, Hierarchy and Generic Java Dynamic Objects: Create Objects Dynamically With Reflec Java Dynamic Proxy: Intercept Method Calls With Proxy and In Java Security Java Secure Coding Java Password Hashing Java Input Validation Java Cryptography Basics Java KeyStore Basics Java TLS Basics Java SecureRandom Java JUnit Basics Java JUnit Test Methods JUnit Assertions JUnit Test Fixtures JUnit Parameterized Tests Mockito in Java Java Mock Objects Java Integration Testing Java Code Coverage Maven in Java Create a Maven Project in Java Maven Dependencies Maven Plugins Gradle in Java Gradle Build Gradle Dependencies Build Executable JAR in Java Build WAR File in Java HTTP Basics in Java Java Servlet Basics Servlet Lifecycle in Java HttpServletRequest in Java HttpServletResponse in Java JSP in Java JSTL in JSP REST API in Java JSON in Java Java Web Application Spring Framework Basics Spring Boot Tutorial Spring MVC Spring Data JPA Spring Security Spring Boot REST API Jakarta EE Basics Java Deployment JAR Deployment Java WAR Deployment Run Java as a Service Docker Java: Dockerize a Java Application Java Environment Variables and Configuration Java Logging Basics Java Production Setup Java JVM Options Java JVM Monitoring Java Hello World App Java Calculator App Java Number Guess Game Java Student Manager Java Library System Java Bank App Java Todo App Java File Manager Java Quiz App Java REST API Project Java Chat Server Java Employee Manager Java E-commerce API
Ask about this post
AI Ask about this post

Ask questions about TreeMap and get answers drawn from it.

Signed-in readers only.