TreeSet
About
TreeSet
is a part of the Java Collections Framework that implements the Set
interface and is backed by a TreeMap
. It is a sorted collection, where elements are arranged in their natural order or according to a custom comparator provided at creation time. Being a NavigableSet
, it provides methods for range searches and navigation.
Features
Sorted Order: Elements are stored in ascending order by default.
Custom Sorting: Supports custom ordering using a
Comparator
.No Duplicates: Ensures all elements are unique.
Implements
NavigableSet
: Provides methods for navigation (e.g.,floor
,ceiling
,higher
,lower
).Tree-Based Structure: Backed by a Red-Black tree.
Non-Synchronized: Not thread-safe. External synchronization is needed for concurrent access.
Null Element: Does not allow
null
in Java 8 and later versions.
Internal Working
1. Backed by TreeMap
Internally,
TreeSet
uses aTreeMap
to store elements as keys, with a constant dummy value (PRESENT
).This ensures elements are unique and sorted.
2. Red-Black Tree
The
TreeMap
is implemented as a Red-Black tree, a self-balancing binary search tree.The tree ensures that:
No two consecutive nodes are red.
The path from the root to the farthest leaf is no more than twice as long as the path to the nearest leaf.
3. Sorting
Natural Order: By default, elements are sorted using their
compareTo
method (from theComparable
interface).Custom Comparator: A
Comparator
can be provided to override the natural ordering.
4. Insertion
New elements are added using the
put
operation of the underlyingTreeMap
.The Red-Black tree rebalances itself during insertion to maintain its properties.
5. Navigation
Methods like
floor
,ceiling
,higher
, andlower
leverage the Red-Black tree's sorted nature to efficiently find and return elements based on specific criteria.
Key Methods
Method
Description
add(E e)
Adds the specified element to the set.
remove(Object o)
Removes the specified element from the set.
contains(Object o)
Returns true
if the set contains the specified element.
first()
Returns the smallest element in the set.
last()
Returns the largest element in the set.
ceiling(E e)
Returns the least element greater than or equal to the given element.
floor(E e)
Returns the greatest element less than or equal to the given element.
higher(E e)
Returns the least element strictly greater than the given element.
lower(E e)
Returns the greatest element strictly less than the given element.
pollFirst()
Retrieves and removes the first (smallest) element.
pollLast()
Retrieves and removes the last (largest) element.
subSet(E from, E to)
Returns a view of the portion of this set whose elements range from from
(inclusive) to to
.
headSet(E to)
Returns a view of the portion of this set whose elements are strictly less than to
.
tailSet(E from)
Returns a view of the portion of this set whose elements are greater than or equal to from
.
Big-O for Operations
Operation
Time Complexity
Add
O(log n)
Remove
O(log n)
Contains
O(log n)
Iteration
O(n)
Limitations
Memory Overhead: Higher memory consumption due to the Red-Black tree structure.
Slower than
HashSet
: Operations likeadd
andremove
are slower compared toHashSet
due to tree rebalancing.Non-Synchronized: Requires external synchronization for concurrent access.
No Null Elements: Does not allow
null
elements, unlike some otherSet
implementations.
Real-World Usage
Sorted Data: Storing elements in sorted order for scenarios like range queries or leaderboard rankings.
Unique Elements: Ensuring uniqueness with a requirement for predictable ordering.
Custom Order Requirements: Applications where elements must follow a custom sort order.
Examples
1. Basic Operations
2. Using Custom Comparator
3. Navigation Operations
4. Subset Operations
Last updated
Was this helpful?