SortedSet

Overview:

  • SortedSet is an interface in Java that extends Set. It maintains elements in ascending order.
  • Elements must be comparable, either through natural ordering (using Comparable) or by providing a custom comparator (using Comparator).

Key Characteristics:

  • Elements are stored in sorted order (ascending by default).
  • Does not allow duplicates (like all Set implementations).
  • Provides methods to retrieve specific subsets, such as the first or last elements.

Methods In SortedSet

MethodsDescription
Comparator<? super E> comparator()Returns the comparator used to order the elements in this set. If the set uses natural ordering, it returns null.
E first()Returns the first (lowest) element currently in the sorted set.
E last()Returns the last (highest) element currently in the sorted set.
SortedSet<E> headSet(E toElement)Returns a view of the portion of this set whose elements are strictly less than the specified toElement.
SortedSet<E> tailSet(E fromElement)Returns a view of the portion of this set whose elements are greater than or equal to the specified fromElement.
SortedSet<E> subSet(E fromElement, E toElement)Returns a view of the portion of this set whose elements range from fromElement (inclusive) to toElement (exclusive).
#
Code Example
Output
Sorted Set: [1, 3, 5, 10]
First Element: 1
Last Element: 10
HeadSet (elements < 5): [1, 3]
TailSet (elements >= 3): [3, 5, 10]

NavigableSet

Overview:

  • NavigableSet extends SortedSet and provides additional methods to navigate through the set, allowing bidirectional navigation.
  • Introduced in Java 6, it offers methods to retrieve elements based on closest matches for searches (such as floor, ceiling, etc.).

Key Characteristics:

  • Allows navigation (in both ascending and descending order).
  • Methods for finding ceiling, floor, higher, and lower elements, which return the nearest match to a given element.
  • Provides a method to get a reverse view of the set.

Methods in NavigableSet

MethodsDescription
E lower(E e)Returns the greatest element in this set strictly less than the given element, or null if there is no such element.
E floor(E e)Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
E ceiling(E e)Returns the smallest element in this set greater than or equal to the given element, or null if there is no such element.
E higher(E e)Returns the smallest element in this set strictly greater than the given element, or null if there is no such element.
E pollFirst()Retrieves and removes the first (lowest) element, or returns null if the set is empty.
E pollLast()Retrieves and removes the last (highest) element, or returns null if the set is empty.
NavigableSet<E> descendingSet()Returns a view of the set in reverse order (descending order).
Iterator<E> descendingIterator()Returns an iterator over the elements in this set, traversing the elements in reverse order.
Code Example
Output
Original Set: [10, 20, 30, 40, 50]
Ceiling(25): 30
Floor(25): 20
Higher(30): 40
Lower(30): 20
Descending Set: [50, 40, 30, 20, 10]

TreeSet

Overview:

  • TreeSet is a concrete class that implements both NavigableSet and SortedSet.
  • It stores elements in a balanced binary search tree (Red-Black Tree), ensuring the elements are sorted in ascending order by default.

Key Characteristics:

  • Elements are automatically sorted (based on natural ordering or a custom comparator).
  • No duplicate elements allowed.
  • Provides all the methods of both SortedSet and NavigableSet.

NOTE:

  • Balanced Binary Search Tree: TreeSet is backed by a Red-Black tree, ensuring that insertions, deletions, and lookups take O(log n) time.
  • Natural Ordering and Custom Comparators: By default, elements are sorted according to their natural order (defined by Comparable). Alternatively, you can pass a Comparator to TreeSet for custom sorting.
  • SortedSet: Maintains elements in ascending order. It is the base interface that provides the ability to retrieve subsets based on range.
  • NavigableSet: Extends SortedSet and provides advanced methods to navigate the set, such as retrieving the closest matches for a given element.
  • TreeSet: A concrete implementation of NavigableSet and SortedSet, backed by a balanced tree, offering efficient sorting and retrieval of elements in both ascending and descending order.
Code Example
Output
Sorted TreeSet: [Apple, Banana, Mango, Orange]
First Element: Apple
Last Element: Orange
Descending Set: [Orange, Mango, Banana, Apple]