NavigableSet interface

In this article, we will discuss NavigableSet interface with all its important methods for navigation support in detail

NavigableSet doesn’t allow duplicate items and maintains sorting order

 

NavigableSet interface:

  • NavigableSet interface is a sub-interface of SortedSet interface (i.e.; NavigableSet extends SortedSet)
  • To represent a group of element/objects as a single unit/entity, where duplicates aren’t allowed and element/objects are stored according to some sorting order
  • It allows only unique element/objects to be inserted
  • It stores element/objects in sorting order
  • NavigableSet interface defines more specific methods for navigation purposes, in addition to inherited methods from Set/SortedSet/Collection interfaces
  • This is introduced in Java 1.6 version for navigation support
  • Present in java.util package and extends java.util.SortedSet interface

19-NavigableSet-interace-in-java

Source: Team BenchResources.Net

 

 

NavigableSet interface methods:

 NavigableSet methods Description
E floor(E e); returns greatest/highest element which is less than or equal to specified element e

null if there is no such element

E lower(E e); returns greatest/highest element which is less than specified element e

null if there is no such element

E ceiling(E e); returns least/lowest element which is greater than or equal to specified element e

null if there is no such element

E higher(E e); returns least/lowest element which is greater than specified element e

null if there is no such element

E pollFirst(); remove & retrieve 1st element
 
null if there is no such element
E pollLast(); remove & retrieve last element
 
null if there is no such element
NavigableSet<E> descendingSet(); returns invoking navigable set in reverse order

 

 

Example to understand NavigableSet specific methods

NavigableSet = [10, 20, 30, 40, 50, 60, 70];

floor(30) = 30
lower(30) = 20
ceiling(40) = 40
higher(40) = 50
pollFirst() = 10
pollLast() = 70
descendingSet() = [70, 60, 50, 40, 30, 20, 10];

 

 

 

NavigableSet example

 

NavigableSetMethods.java

package in.bench.resources.java.collection;

import java.util.NavigableSet;
import java.util.TreeSet;

public class NavigableSetMethods {

	public static void main(String[] args) {

		// creating NavigableSet reference and TreeSet object
		NavigableSet<String> ns = new TreeSet<String>();

		// adding elements to NavigableSet/TreeSet object
		ns.add("Sundar Pichai");
		ns.add("Satya Nadella");  
		ns.add("Shiv Nadar");  
		ns.add("Shantanu Narayen");
		ns.add("Sundar Pichai"); // adding duplicate element
		ns.add("Francisco D’Souza");
		ns.add("Vishal Sikka");
		ns.add("Chanda Kochhar");

		// natural ordering of elements
		System.out.println("Elements Sorted acc to Natural ordering :\n");
		System.out.println(ns);

		// floor and lower methods
		System.out.println("\n\nfloor(Satya Nadella)  : " 
				+ ns.floor("Satya Nadella"));
		System.out.println("\n\nlower(Satya Nadella)  : " 
				+ ns.lower("Satya Nadella"));

		// ceiling and higher methods
		System.out.println("\n\nceiling(Shantanu Narayen)  : " 
				+ ns.ceiling("Shantanu Narayen"));
		System.out.println("\n\nhigher(Shantanu Narayen)  : " 
				+ ns.higher("Shantanu Narayen"));

		// pollFirst method
		System.out.println("\n\npollFirst() : " 
				+ ns.pollFirst());
		System.out.println("\nNavigableSet after polling First : \n" 
				+ ns);

		// pollLast method
		System.out.println("\n\npollLast() : " + ns.pollLast());
		System.out.println("\nNavigableSet after polling Last : \n" 
				+ ns);

		System.out.println("\n\nNavigableSet in descending order : \n" 
				+ ns.descendingSet());
	}
}

Output:

Elements Sorted acc to Natural ordering : 

[Chanda Kochhar, Francisco D’Souza, Satya Nadella, Shantanu Narayen, 
Shiv Nadar, Sundar Pichai, Vishal Sikka]


floor(Satya Nadella)  : Satya Nadella


lower(Satya Nadella)  : Francisco D’Souza


ceiling(Shantanu Narayen)  : Shantanu Narayen


higher(Shantanu Narayen)  : Shiv Nadar


pollFirst() : Chanda Kochhar

NavigableSet after polling First : 
[Francisco D’Souza, Satya Nadella, Shantanu Narayen, Shiv Nadar, 
Sundar Pichai, Vishal Sikka]


pollLast() : Vishal Sikka

NavigableSet after polling Last : 
[Francisco D’Souza, Satya Nadella, Shantanu Narayen, 
Shiv Nadar, Sundar Pichai]


NavigableSet in descending order : 
[Sundar Pichai, Shiv Nadar, Shantanu Narayen, 
Satya Nadella, Francisco D’Souza]

 

Note: All methods of NavigableSet is non-synchronized but it can be easily converted into synchronized NavigableSet using utility method of java.util.Collections class

Set set = Collections.synchronizedSet(ns);

 

 

Class that implements NavigableSet interface

  • TreeSet implements NavigableSet interface

 

Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/sorted-set.html
https://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html
https://docs.oracle.com/javase/7/docs/api/java/util/NavigableSet.html
https://docs.oracle.com/javase/7/docs/api/java/util/class-use/NavigableSet.html

 

Happy Coding !!
Happy Learning !!