Introduction
The NavigableSet
interface in Java is a part of the java.util
package.
It extends the SortedSet
interface to provide navigation methods that return the closest matches for given search targets. It is commonly implemented by classes such as TreeSet
.
Table of Contents
- What is the
NavigableSet
Interface? - Common Methods
- Examples of Using the
NavigableSet
Interface - Conclusion
1. What is the NavigableSet Interface?
The NavigableSet
interface extends the SortedSet
interface and provides methods to navigate the set and retrieve elements based on closest matches. It allows for greater flexibility in searching and navigating through the set compared to a regular SortedSet
.
2. Common Methods
lower(E e)
: Returns the greatest element in this set strictly less than the given element, ornull
if there is no such element.floor(E e)
: Returns the greatest element in this set less than or equal to the given element, ornull
if there is no such element.ceiling(E e)
: Returns the least element in this set greater than or equal to the given element, ornull
if there is no such element.higher(E e)
: Returns the least element in this set strictly greater than the given element, ornull
if there is no such element.pollFirst()
: Retrieves and removes the first (lowest) element, or returnsnull
if this set is empty.pollLast()
: Retrieves and removes the last (highest) element, or returnsnull
if this set is empty.descendingSet()
: Returns a reverse order view of the elements contained in this set.subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
: Returns a view of the portion of this set whose elements range fromfromElement
totoElement
.
3. Examples of Using the NavigableSet Interface
Example 1: Basic Usage of NavigableSet
This example demonstrates how to use a NavigableSet
to perform basic operations.
import java.util.NavigableSet;
import java.util.TreeSet;
public class NavigableSetExample {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
set.add(1);
set.add(2);
set.add(3);
System.out.println("Set: " + set);
System.out.println("Lower (2): " + set.lower(2));
System.out.println("Floor (2): " + set.floor(2));
System.out.println("Ceiling (2): " + set.ceiling(2));
System.out.println("Higher (2): " + set.higher(2));
}
}
Output:
Set: [1, 2, 3]
Lower (2): 1
Floor (2): 2
Ceiling (2): 2
Higher (2): 3
Example 2: Polling Elements
This example shows how to use pollFirst
and pollLast
methods.
import java.util.NavigableSet;
import java.util.TreeSet;
public class PollingElementsExample {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
set.add(1);
set.add(2);
set.add(3);
System.out.println("Initial Set: " + set);
System.out.println("Poll First: " + set.pollFirst());
System.out.println("Set after polling first: " + set);
System.out.println("Poll Last: " + set.pollLast());
System.out.println("Set after polling last: " + set);
}
}
Output:
Initial Set: [1, 2, 3]
Poll First: 1
Set after polling first: [2, 3]
Poll Last: 3
Set after polling last: [2]
Example 3: Descending Set
This example demonstrates how to get a reverse order view of the set using the descendingSet
method.
import java.util.NavigableSet;
import java.util.TreeSet;
public class DescendingSetExample {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
set.add(1);
set.add(2);
set.add(3);
NavigableSet<Integer> descendingSet = set.descendingSet();
System.out.println("Original Set: " + set);
System.out.println("Descending Set: " + descendingSet);
}
}
Output:
Original Set: [1, 2, 3]
Descending Set: [3, 2, 1]
Example 4: Using subSet
This example demonstrates how to use the subSet
method to get a view of a portion of the set.
import java.util.NavigableSet;
import java.util.TreeSet;
public class SubSetExample {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
set.add(1);
set.add(2);
set.add(3);
set.add(4);
set.add(5);
NavigableSet<Integer> subSet = set.subSet(2, true, 4, true);
System.out.println("SubSet from 2 (inclusive) to 4 (inclusive): " + subSet);
}
}
Output:
SubSet from 2 (inclusive) to 4 (inclusive): [2, 3, 4]
Example 5: Using headSet
and tailSet
This example shows how to use headSet
and tailSet
to get a view of a portion of the set.
import java.util.NavigableSet;
import java.util.TreeSet;
public class HeadSetTailSetExample {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
set.add(1);
set.add(2);
set.add(3);
set.add(4);
set.add(5);
NavigableSet<Integer> headSet = set.headSet(3, true);
NavigableSet<Integer> tailSet = set.tailSet(3, true);
System.out.println("HeadSet up to 3 (inclusive): " + headSet);
System.out.println("TailSet from 3 (inclusive): " + tailSet);
}
}
Output:
HeadSet up to 3 (inclusive): [1, 2, 3]
TailSet from 3 (inclusive): [3, 4, 5]
4. Conclusion
The NavigableSet
interface in Java provides powerful methods for navigating and manipulating sorted sets. It extends the functionality of the SortedSet
interface by offering methods to find the closest matches for given search targets, poll elements, and view portions of the set. The examples provided demonstrate common usage patterns and highlight the capabilities of the NavigableSet
interface.
Comments
Post a Comment
Leave Comment