The main difference is SortedSet offers basic operations in a sorted order, while NavigableSet provides more advanced navigational operations.
Criteria | NavigableSet | SortedSet |
---|---|---|
Interface Hierarchy | NavigableSet extends SortedSet. | SortedSet is a subinterface of Set. |
Ordering | Guarantees element ordering, like SortedSet. | Guarantees element ordering. |
Primary Methods | Includes methods like lower(), floor(), ceiling(), higher(), pollFirst(), and pollLast() for navigational operations. | Limited to methods like first(), last(), subSet(), headSet(), and tailSet(). |
View Operations | Supports both ascending and descending views of the set using methods like descendingSet(). | Does not have descending view capabilities. |
Use-Case | When enhanced navigational operations are needed in addition to sorted operations. | When you simply need a set with sorted elements. |
Implementations in Java Collections Framework | TreeSet is a notable implementation. | TreeSet implements both SortedSet and NavigableSet, but other custom implementations can limit themselves to just SortedSet methods. |
Thread Safety | Standard implementations like TreeSet are not thread-safe. Synchronization must be handled externally if concurrent modifications are expected. | Same as NavigableSet. |
Example: Difference Between NavigableSet and SortedSet in Java
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;
public class NavigableSetVsSortedSetDemo {
public static void main(String[] args) {
// Using a SortedSet
SortedSet<String> sortedSet = new TreeSet<>();
sortedSet.add("A");
sortedSet.add("B");
sortedSet.add("C");
// Using a NavigableSet
NavigableSet<String> navigableSet = new TreeSet<>();
navigableSet.add("A");
navigableSet.add("B");
navigableSet.add("C");
System.out.println("SortedSet First Element: " + sortedSet.first());
System.out.println("NavigableSet First Element: " + navigableSet.first());
// Demonstrating a feature of NavigableSet
System.out.println("NavigableSet Lower Element for 'B': " + navigableSet.lower("B"));
}
}
Output:
SortedSet First Element: A NavigableSet First Element: A NavigableSet Lower Element for 'B': A
Explanation:
1. SortedSet: It is an interface in Java that extends Set. It ensures that the elements are maintained in ascending order. The SortedSet provides methods like first() and last() to retrieve the first and last elements.
2. NavigableSet: It is a sub-interface of SortedSet and provides several methods that allow for more flexible navigation, like lower(), floor(), ceiling(), and higher().
3. In the provided example:
– Both SortedSet and NavigableSet have the same ordering of elements and both can retrieve the first element using the first() method.
– The NavigableSet demonstrates its unique capability with the lower() method which returns the greatest element strictly less than the given element.
4. When to use:
– Use SortedSet when you just need to ensure that the elements are in a sorted order and want to retrieve basic boundaries like the first and last element.
– Use NavigableSet when you need advanced navigation methods to navigate through the set based on proximity to a given element.
In summary, while SortedSet offers basic operations in a sorted order, NavigableSet provides more advanced navigational operations.