Java TreeSet: Red-Black Balanced Trees & NavigableSet Operations

TreeSet implements the NavigableSet interface, backed internally by a self-balancing Red-Black Binary Search Tree. It ensures that elements are stored strictly unique and maintained in ascending sorted order.

Code Demonstration: NavigableSet Navigation Methods

import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet<Integer> numbers = new TreeSet<>();
        numbers.add(50);
        numbers.add(20);
        numbers.add(80);
        numbers.add(10);
        numbers.add(90);

        // Elements automatically sorted: [10, 20, 50, 80, 90]
        System.out.println("Sorted TreeSet: " + numbers);

        // 1. Boundary Queries:
        System.out.println("Smallest (first): " + numbers.first()); // 10
        System.out.println("Largest (last):   " + numbers.last());  // 90

        // 2. NavigableSet Proximity Queries:
        System.out.println("Higher than 50 (strictly >):  " + numbers.higher(50));  // 80
        System.out.println("Lower than 50 (strictly <):   " + numbers.lower(50));   // 20
        System.out.println("Ceiling of 45 (>=):           " + numbers.ceiling(45)); // 50
        System.out.println("Floor of 45 (<=):             " + numbers.floor(45));   // 20

        // 3. SubSet Range Extraction:
        System.out.println("Subset [20 to 80): " + numbers.subSet(20, 80)); // [20, 50]
    }
}

HashSet vs. LinkedHashSet vs. TreeSet

Feature HashSet LinkedHashSet TreeSet
Ordering None (Random) Insertion Order Sorted Order (Natural / Comparator)
Time Complexity O(1) Constant O(1) Constant O(log n) Logarithmic
Underlying Structure Hash Table (HashMap) Hash Table + Doubly-linked list Red-Black Binary Search Tree
Permits Null? Yes (1 null) Yes (1 null) No (Throws NullPointerException)

Next Step

Now let us explore the most critical key-value data structure in Java: HashMap. Proceed to: Java HashMap & LinkedHashMap →

Frequently Asked Questions (FAQ)

Q1. What data structure does TreeSet use internally?
TreeSet is internally backed by a TreeMap, which implements a self-balancing Red-Black Binary Search Tree. This guarantees O(log n) time complexity for add, remove, and search operations.
Q2. Can TreeSet store null values in Java?
No! Since Java 7, inserting 'null' into a TreeSet throws a NullPointerException because elements must be compared with existing nodes via compareTo() or compare().
Q3. What is the NavigableSet interface in Java?
NavigableSet (introduced in Java 6 and extended by TreeSet) provides advanced navigation queries, such as higher(e) (strictly greater), lower(e) (strictly less), ceiling(e) (greater or equal), and floor(e) (less or equal).

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java HashSet →

O(1) unordered set alternative.

Java TreeMap →

The underlying backing store of TreeSet.

Comparable vs Comparator →

Defining sorting logic.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!