B-Tree Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓B-Tree runs in O(log n) time and O(n) space, in Java as in every other language.
- ✓java.util.TreeMap and TreeSet, which are red-black trees rather than B-trees. There is no B-tree anywhere in the JDK.
- ✓Use TreeMap for ordered in-memory maps and reach for the NavigableMap methods rather than iterating and filtering, since floorEntry and ceilingEntry are the operations the tree structure actually accelerates.
- ✓The Java trap: TreeMap decides key identity with compareTo, not equals, so a comparator that returns zero for two objects that are not equal makes them the same key: the second put overwrites the first and no exception is raised.
- ✓Each entry is an object holding a key reference, a value reference, left, right and parent references plus a colour flag, which is roughly forty-eight bytes to store one mapping, and the entries are scattered across the heap.
What B-Tree actually is
B-Tree in Java: what you actually reach for
Java implementation
import java.util.NavigableMap;
import java.util.TreeMap;
public final class OrderedIndex {
// TreeMap is a RED-BLACK tree, not a B-tree. The JDK has no B-tree.
private final NavigableMap<Long, String> index = new TreeMap<>();
public void put(long key, String value) {
index.put(key, value); // NPE on a null key, unlike HashMap
}
// These are the methods that justify a tree over a HashMap.
public String atOrBelow(long key) {
var e = index.floorEntry(key);
return e == null ? null : e.getValue();
}
public String atOrAbove(long key) {
var e = index.ceilingEntry(key);
return e == null ? null : e.getValue();
}
public NavigableMap<Long, String> between(long lo, long hi) {
return index.subMap(lo, true, hi, true); // a VIEW, not a copy
}
// The trap: a comparator inconsistent with equals makes distinct keys
// collide, and the second put silently overwrites the first.
// new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER)
// put("Key", 1); put("KEY", 2); -> size() == 1, value 2
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for B-Tree?
java.util.TreeMap and TreeSet, which are red-black trees rather than B-trees. There is no B-tree anywhere in the JDK. TreeMap implements NavigableMap, so floorKey, ceilingKey, headMap, tailMap and subMap give the ordered queries.
What is the most common Java mistake when implementing B-Tree?
TreeMap decides key identity with compareTo, not equals, so a comparator that returns zero for two objects that are not equal makes them the same key: the second put overwrites the first and no exception is raised. The JDK documentation calls this being inconsistent with equals and notes that such a map simply does not obey the Map interface contract. It surfaces as a map that silently loses entries, and swapping in a HashMap to debug it makes the symptom vanish, which sends people looking in the wrong place. TreeMap also throws NullPointerException on a null key, while HashMap accepts one.
How does B-Tree actually perform in Java?
Each entry is an object holding a key reference, a value reference, left, right and parent references plus a colour flag, which is roughly forty-eight bytes to store one mapping, and the entries are scattered across the heap. A B-tree would touch fewer cache lines, but the JDK does not offer one. Time complexity is O(log n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for GitHub Copilot, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.