Last Updated on 16 June 2019   |   Print Email
In this article, we’re going to help you understand the NavigabeSetinterface in the Java Collections Framework with code examples using TreeSet. Besides Set and SortedSet, TreeSet also implements NavigableSet.
1. Understanding NavigableSet
NavigableSet is a sub interface of the SortedSet interface, so it inherits all SortedSet’s behaviors like range view, endpoints and comparator access. In addition, the NavigableSet interface provides navigation methods and descending iterator that allows the elements in the set can be traversed in descending order.Let’s look at each new method defined by this interface in details.
lower(E): returns the greatest element which is less than the specified element E, or null if there is no such element.
floor(E): returns the greatest element which is less than or equal to the given element E.
ceiling(E): returns the least element which is greater than or equal to the given element E.
higher(E): returns the least element which is strictly greater than the specified element E.
descendingSet(): returns a reverse order view of the elements contained in the set.
descendingIterator(): returns an iterator that allows traversing over elements in the set in descending order.
pollFirst(): retrieves and removes the first (lowest) element, or returns null if the set is empty.
pollLast(): retrieves and removes the last (highest) element, or returns null if the set is empty.
Furthermore, the NavigableSet interface overloads the methods headSet(), subSet() and tailSet() from the SortedSet interface, which accepts additional arguments describing whether lower or upper bounds are inclusive versus exclusive:
headSet(E toElement, boolean inclusive)
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
tailSet(E fromElement, boolean inclusive)
Now, let’s look at some code examples.
2. NavigableSet Code Examples
The following example shows how to obtain a reverse order set from the original one:
Set Fruits: [Apple, Banana, Grape, Mango, Orange]
Fruits by descending order: Orange, Mango, Grape, Banana, Apple,
The following example demonstrates the benefits of using navigation methods like lower(), higher(), ceiling() and floor(); and range view methods like headSet(), subSet() and tailSet().Given the following entity class:
/**
* Employee.java
* NavigableSet Example
* @author www.codejava.net
*/
class Employee {
String name;
int salary;
public Employee(int salary) {
this.salary = salary;
}
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
public String toString() {
return this.name + "-" + salary;
}
public String getName() {
return this.name;
}
public Integer getSalary() {
return new Integer(this.salary);
}
public boolean equals(Object obj) {
if (obj instanceof Employee) {
Employee another = (Employee) obj;
if (this.name.equals(another.name)
&& this.salary == another.salary) {
return true;
}
}
return false;
}
public int hashCode() {
return 31 * name.hashCode() + salary;
}
}
Note that this class overrides the equals() and hashCode() methods based on employee’s name and salary. The following comparator class compares two employees based on their salary:
public class EmployeeComparator implements Comparator<Employee> {
public int compare(Employee emp1, Employee emp2) {
return emp1.getSalary().compareTo(emp2.getSalary());
}
}
We add 8 employees into a navigable set like this:
Here, an employee object is printed with name and salary. * Using the higher() method, we can know the employee whose salary is higher than the employee ‘Tom’:
Employee Tom = new Employee("Tom", 80000);
Employee emp1 = setEmployees.higher(Tom);
if (emp1 != null) {
System.out.println("The employee whose salary > Tom: " + emp1);
}
Output:
The employee whose salary > Tom: Sam-82000
NOTE: to allow this kind of search possible, the entity class must correctly override the equals() and hashCode() method, as shown in the Employee class above. * Using the lower() method, we can know the employee whose salary is less than the employee Tom:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments