In Java, subMap(fromKey, toKey) returns the part of a TreeMap whose keys lie from fromKey (inclusive) up to toKey (exclusive). The four-argument form, added in Java 6, lets you choose whether each end is inclusive.
The result is a live view, not a copy: it shares storage with the map, so changes on either side are visible in the other, and creating it costs only O(log n) per lookup. The two-argument form returns SortedMap; the four-argument form returns NavigableMap.
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
Give the lower and upper bound; optionally say whether each is included.
Java
TreeMap<Integer, Integer> sales = new TreeMap<>();
SortedMap<Integer, Integer> a = sales.subMap(3, 7); // keys 3 <= k < 7
NavigableMap<Integer, Integer> b = sales.subMap(3, true, 7, true); // keys 3 <= k <= 7
Call
What it does
map.subMap(from, to)
Keys in [from, to): from included, to excluded.
map.subMap(from, true, to, true)
Keys in [from, to]: both ends included.
map.subMap(from, false, to, false)
Keys strictly between the two bounds.
For String keys, subMap("A", "N") is a neat way to get every key that starts with A to M: any word beginning with N sorts after the one-letter string "N".
Library declarations
Java
// java.util.TreeMap<K, V>
public SortedMap<K, V> subMap(K fromKey, K toKey)
public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive) // Java 6
Parameters
Parameter
Description
fromKey (K)
Low end of the range; included unless fromInclusive is false.
toKey (K)
High end of the range; excluded unless toInclusive is true.
fromInclusive, toInclusive (boolean)
Four-argument form only: whether each bound belongs to the view.
Return Value
Return Value
Description
SortedMap<K, V>
Two-argument form: a live view of the half-open range.
NavigableMap<K, V>
Four-argument form: a live view with navigation methods.
subMap("A", "N") picks every name starting with A through M.
Java
import java.util.SortedMap;
import java.util.TreeMap;
public class TreeMapSubMapAToM {
public static void main(String[] args) {
TreeMap<String, Integer> contacts = new TreeMap<>();
contacts.put("Mona", 240);
contacts.put("Alba", 201);
contacts.put("Zara", 377);
contacts.put("Kofi", 118);
contacts.put("Nils", 402);
contacts.put("Bea", 315);
SortedMap<String, Integer> firstHalf = contacts.subMap("A", "N");
System.out.println("A to M: " + firstHalf);
System.out.println("first: " + firstHalf.firstKey() + ", last: " + firstHalf.lastKey());
System.out.println("names in range: " + firstHalf.size() + " of " + contacts.size());
}
}
Output
A to M: {Alba=201, Bea=315, Kofi=118, Mona=240}
first: Alba, last: Mona
names in range: 4 of 6
How It Works
An upper bound that is not a name. "N" is not in the map, yet it works as a fence: "Mona" sorts before it while "Nils" sorts after it, because a longer string with the same first letter is greater.
The view has its own ends.firstKey() and lastKey() on the view report Alba and Mona, the ends of the range rather than of the whole map.
Counting is range-aware.size() on the view gives 4 out of the 6 contacts; Nils and Zara lie outside.
Example 2 — Half-Open, Closed, and Open Ranges
Units sold per day, viewed through three versions of "days 3 to 7".
middle: {25=Bo}
after seats.put(22): {22=Dev, 25=Bo}
after middle.remove(25): {10=Ann, 22=Dev, 40=Cy}
middle.put(35): IllegalArgumentException
subMap(30, 20): IllegalArgumentException
How It Works
Map to view. Seat 22 was added to seats after the view was created, and it appears in middle because 22 lies in [20, 30).
View to map. Removing 25 through the view deletes Bo from the real map; the view stores nothing of its own.
Two kinds of out of range. Seat 35 is outside the window, so middle.put is rejected, and subMap(30, 20) fails because its lower bound is above its upper bound.
Frequently Asked Questions
Not in the two-argument form: subMap(from, to) includes from and excludes to. Use subMap(from, true, to, true) to include both ends.
No. It returns a live view backed by the map. Wrap it in new TreeMap<>(view) if you need an independent copy.
Either fromKey is greater than toKey, or you tried to put a key outside the view's range.
subMap(from, to) returns SortedMap with fixed bounds; subMap(from, fromInclusive, to, toInclusive) returns NavigableMap and lets you choose each bound.
Yes. map.subMap(a, b).clear() removes every key in [a, b) from the backing map.