Java TreeMap & TreeSet
In this page:
Introduction to TreeMap
A TreeMap is a Map implementation that keeps its keys continuously sorted, either by their natural ordering or by a custom Comparator you supply. Unlike a HashMap, iterating a TreeMap always visits keys in sorted order, at the cost of slightly slower average-case operations.
Example: Introduction to TreeMap
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, Integer> map = new TreeMap<>();
map.put("banana", 2);
map.put("apple", 1);
map.put("cherry", 3);
System.out.println(map); // iterates in sorted key order
}
}
Login to try C/C++/Java/PHP code in the editor
Navigating TreeMap Methods
TreeMap implements the NavigableMap interface, which adds a rich set of methods for finding the nearest matching key relative to a given value (like floorKey or ceilingKey), or for retrieving a sorted sub-range of the map's entries directly.
Example: Navigating TreeMap Methods
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>();
map.put(10, "ten"); map.put(20, "twenty"); map.put(30, "thirty");
System.out.println(map.floorKey(25));
System.out.println(map.ceilingKey(25));
}
}
Login to try C/C++/Java/PHP code in the editor
Introduction to TreeSet
A TreeSet is the Set equivalent of a TreeMap: it automatically keeps its elements in ascending sorted order and, like every Set, rejects duplicate entries outright.
Example: Introduction to TreeSet
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(5); set.add(1); set.add(3); set.add(1);
System.out.println(set); // sorted, duplicates rejected
}
}
Login to try C/C++/Java/PHP code in the editor
Navigating TreeSet Methods
TreeSet implements the NavigableSet interface, offering the same kind of nearest-match and sub-range query methods that NavigableMap provides for TreeMap, letting you efficiently find elements around a given point without scanning the whole set.
Example: Navigating TreeSet Methods
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(10); set.add(20); set.add(30);
System.out.println(set.higher(15));
System.out.println(set.headSet(25));
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Sorting in Tree Collections
You can customize how elements are ordered in either Tree collection by passing a custom Comparator into its constructor at creation time, overriding the elements' natural ordering entirely, which is essential when the element type doesn't implement Comparable or you want a different order than its default.
Example: Custom Sorting in Tree Collections
import java.util.TreeSet;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
TreeSet<String> set = new TreeSet<>(Comparator.reverseOrder());
set.add("apple"); set.add("banana"); set.add("cherry");
System.out.println(set);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: