Trie
Java
Map Implementation
Data Structures
Java Libraries

Where do I find a standard Trie based map implementation in Java?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Java's standard library does not include a built-in trie map implementation. If you need prefix-tree behavior, you have three realistic options: implement a trie yourself, use a third-party library, or step back and ask whether a sorted map is good enough for your use case.

The short answer: not in the JDK

The Java Collections Framework gives you HashMap, TreeMap, LinkedHashMap, and related types, but not a trie-based map.

That means there is no standard TrieMap in java.util that you can import and use directly. If someone asks for a "standard Java trie," the precise answer is that the standard JDK does not provide one.

When you actually need a trie

A trie is useful when the main operations are prefix-oriented rather than exact-key lookups. Common examples include:

  • autocomplete
  • dictionary prefix search
  • longest-prefix matching
  • routing or token lookup by shared prefixes

If your main workload is just exact key lookup, HashMap is usually simpler and faster.

If your workload is ordered key lookup with range queries, TreeMap may already solve the real problem without the complexity of a trie.

A minimal trie map in Java

If you need true trie behavior, implementing a small one is often straightforward.

java
1import java.util.HashMap;
2import java.util.Map;
3
4public class TrieMap<V> {
5    private static class Node<V> {
6        Map<Character, Node<V>> children = new HashMap<>();
7        V value;
8        boolean terminal;
9    }
10
11    private final Node<V> root = new Node<>();
12
13    public void put(String key, V value) {
14        Node<V> current = root;
15        for (char ch : key.toCharArray()) {
16            current = current.children.computeIfAbsent(ch, c -> new Node<>());
17        }
18        current.terminal = true;
19        current.value = value;
20    }
21
22    public V get(String key) {
23        Node<V> current = root;
24        for (char ch : key.toCharArray()) {
25            current = current.children.get(ch);
26            if (current == null) {
27                return null;
28            }
29        }
30        return current.terminal ? current.value : null;
31    }
32
33    public boolean containsPrefix(String prefix) {
34        Node<V> current = root;
35        for (char ch : prefix.toCharArray()) {
36            current = current.children.get(ch);
37            if (current == null) {
38                return false;
39            }
40        }
41        return true;
42    }
43}

This is not a production-ready complete map implementation, but it demonstrates the structure clearly.

Third-party libraries exist

If you do not want to implement the structure yourself, third-party libraries are the normal route. One commonly mentioned option is Apache Commons Collections' PatriciaTrie, which is a compressed trie suited to string-key prefix lookups.

That said, do not assume every third-party trie behaves like a full Map<String, V> in every respect. Read the API carefully and make sure its key model, prefix operations, and performance characteristics match what you need.

Sometimes TreeMap is enough

A lot of "I need a trie" problems are actually "I need fast prefix iteration." In some cases, a TreeMap plus range queries on string keys is sufficient and much easier to maintain.

So before introducing a trie, ask:

  • do I need prefix search or just sorted lookups
  • do I need memory efficiency for many shared prefixes
  • do I need exact Map semantics or specialized prefix operations

That decision usually matters more than the data structure's name.

Common Pitfalls

The biggest pitfall is assuming the JDK has a built-in trie and losing time searching for it. It does not.

Another issue is choosing a trie when a simpler structure such as HashMap or TreeMap would already meet the requirements.

It is also easy to underestimate implementation details such as deletion, prefix iteration, and memory overhead. A basic trie is simple to sketch, but a polished trie-based map is more work than it first appears.

Finally, if you use a third-party implementation, verify its maintenance status and API behavior before committing to it in a long-lived codebase.

Summary

  • The standard Java library does not include a built-in trie map.
  • Use a trie only when prefix-oriented operations are central to the problem.
  • Implement a small trie yourself if the requirements are narrow and well understood.
  • Consider third-party libraries such as Patricia trie implementations when you need more features.
  • Re-evaluate whether HashMap or TreeMap would solve the real problem more simply.

Course illustration
Course illustration

All Rights Reserved.