0% found this document useful (0 votes)
2 views

TreeSet in Java

TreeSet is a part of the Java Collection Framework that implements the NavigableSet interface, storing elements in sorted order without duplicates. It offers O(log n) time complexity for operations like insertion and deletion, and supports various navigation methods. TreeSet can also utilize custom comparators for sorting, and is not synchronized, requiring additional measures for thread safety.

Uploaded by

azkiyahuda711
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

TreeSet in Java

TreeSet is a part of the Java Collection Framework that implements the NavigableSet interface, storing elements in sorted order without duplicates. It offers O(log n) time complexity for operations like insertion and deletion, and supports various navigation methods. TreeSet can also utilize custom comparators for sorting, and is not synchronized, requiring additional measures for thread safety.

Uploaded by

azkiyahuda711
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TreeSet in Java

1. What is TreeSet?
 TreeSet is a part of the Java Collection Framework, under the java.util package.
 It implements the NavigableSet interface, which extends SortedSet, ensuring
elements are stored in sorted order (ascending by default).
 No duplicate elements are allowed.
 It is ordered and sorted using natural ordering (Comparable) or a custom comparator
(Comparator).
 Not synchronized (not thread-safe).

2. Key Features of TreeSet


 Sorted Collection – Stores elements in ascending order by default.
 No Duplicates – Ensures only unique elements are stored.
 Implements NavigableSet – Supports methods like higher(), lower(), ceiling(), and
floor().
 Balanced Tree (Red-Black Tree Implementation) – Provides O(log n) time
complexity for insertion, deletion, and search.
 Not Synchronized – Use Collections.synchronizedSet() for thread safety.

3. Example Program: Using TreeSet


import java.util.TreeSet;

public class TreeSetExample {


public static void main(String[] args) {
// Creating a TreeSet
TreeSet<Integer> numbers = new TreeSet<>();

// Adding elements
numbers.add(40);
numbers.add(10);
numbers.add(30);
numbers.add(20);
numbers.add(50);
numbers.add(10); // Duplicate, won't be added

// Displaying elements (Sorted Order)


System.out.println("TreeSet elements (Sorted): " + numbers);

// Checking if an element exists


System.out.println("Contains 30? " + numbers.contains(30));

// Removing an element
numbers.remove(20);
System.out.println("After removing 20: " + numbers);

// First and Last elements


System.out.println("First element: " + numbers.first());
System.out.println("Last element: " + numbers.last());

// Finding closest values


System.out.println("Higher than 30: " + numbers.higher(30));
System.out.println("Lower than 30: " + numbers.lower(30));

// Iterating using for-each loop


System.out.println("Iterating over TreeSet:");
for (Integer num : numbers) {
System.out.println(num);
}

// Checking size
System.out.println("Size of TreeSet: " + numbers.size());

// Clearing the TreeSet


numbers.clear();
System.out.println("Is TreeSet empty? " + numbers.isEmpty());
}
}

4. Output
TreeSet elements (Sorted): [10, 20, 30, 40, 50]
Contains 30? true
After removing 20: [10, 30, 40, 50]
First element: 10
Last element: 50
Higher than 30: 40
Lower than 30: 10
Iterating over TreeSet:
10
30
40
50
Size of TreeSet: 4
Is TreeSet empty? true

5. Important Methods in TreeSet

Method Description

add(E e) Adds an element to the TreeSet.

remove(Object o) Removes the specified element.

contains(Object o) Checks if an element exists in the TreeSet.

size() Returns the number of elements.

first() Returns the first (lowest) element.

last() Returns the last (highest) element.

higher(E e) Returns the next higher element.


Method Description

lower(E e) Returns the next lower element.

ceiling(E e) Returns the least element greater than or equal to e.

floor(E e) Returns the greatest element less than or equal to e.

clear() Removes all elements.

6. Using TreeSet with Custom Comparator


If you want to store elements in descending order, you can use a custom comparator:
import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDescending {


public static void main(String[] args) {
// Creating a TreeSet with a custom Comparator for Descending Order
TreeSet<Integer> numbers = new TreeSet<>(Comparator.reverseOrder());

numbers.add(10);
numbers.add(50);
numbers.add(30);
numbers.add(20);
numbers.add(40);

System.out.println("TreeSet in Descending Order: " + numbers);


}
}
Output:
TreeSet in Descending Order: [50, 40, 30, 20, 10]

7. When to Use TreeSet?


 When sorting is required (default or custom order).
 When you need fast retrieval (O(log n) time complexity).
 When you need navigation methods like higher(), lower(), ceiling(), and floor().

You might also like