NavigableSet , NavigableMap and Collection Sort in Java

1.6 version enhancements in Collection Framework: As a part of 1.6 version, the following two concepts were introduced in Collection Framework:

1) Navigable Set
2) Navigable Map.


Navigable Set

It is the child interface of SortedSet and it defines several methods for Navigation purposes.

Navigable Set Methods

NavigableSet defines the following methods:

  • floor(e): This method returns the highest element which is less than or equal to e.
  • lower(e): This method returns the highest element which is less than e.
  • ceiling(e): This method returns the lowest element which is greater than or equal to e.
  • higher(e):  This method returns the lowest element which is greater than e.
  • pollFirst(): This method removes and returns the first element.
  • pollLast(): This method removes and returns the last element.
  • descendingSet(): This method returns NavigableSet in reverse order.

Now let’s take a simple example for NavigableSet:

import java.util.*;
class NavigDemo
{
    public static void main(String[] args)
    {
        TreeSet t = new TreeSet();

// As TreeSet is the only implementational class of NavigableSet
// So we have to create TreeSet Object

        t.add(4589);
        t.add(7853);
        t.add(2556);
        t.add(7536);
        t.add(4258);
        System.out.println(t);
        System.out.println(t.floor(4000));
        System.out.println(t.ceiling(3000));
        System.out.println(t.higher(2000));
        System.out.println(t.lower(4000));
    }
}

If we run the above code we will get the following output:

[2556, 4258, 4589, 7536, 7853]
2556
4258
2556
2556

Navigable Map

NavigableMap is the child interface of SortedMap. It defines several methods for Navigation purposes.

Navigable Map Methods

NavigableMap defines the following methods:

  • floorKey(e): This method returns the highest element which is less than or equal to e.
  • ceilingKey(e): This method returns the lowest element which is greater than or equal to e.
  • lowerKey(e): This method returns the highest element which is less than e.
  • higherKey(e): This method returns the lowest element which is greater than e.
  • pollFirstEntry(): This method removes and returns the first element.
  • pollLastEntry(): This method removes and returns the last element.
  • descendingMap(): This method returns the map in reverse order.

Now let’s take a sample program example for NavigableMap:

import java.util.*;
class NavigmDemo
{
    public static void main(String[] args)
    {
        TreeMap t = new TreeMap();

// As TreeMap is the only implementational class of NavigableMap
// So we have to create TreeMap Object

        t.put("A","8585");
        t.put("B","2655");
        t.put("C","3254");
        t.put("D","6598");
        t.put("E","5418");
        System.out.println(t);
        System.out.println(t.floorKey("B"));
        System.out.println(t.ceilingKey("C"));
        System.out.println(t.higherKey("D"));
        System.out.println(t.lowerKey("C"));
    }
}

When we run the above code we will get the folowing output:

{A=8585, B=2655, C=3254, D=6598, E=5418}
B
C
E
B

Collections Class defines several utility methods for Collection objects like sorting, searching, reversing etc.

Sorting elements of List:

  • public static void sort(List t):
    Elements present in the list will be sorted according to default natural sorting order. But, when we are depending on default natural sorting order, elements must be homogeneous and comparable. If elements are not homogeneous and comparable then we will get ClassCastException. Again if the list contains null then we will get NullPointerException, so the list should not contain null.
  • public static void sort(List l, Comparator c):
    Elements present in the list will be sorted according to the customized sorting order specified by comparator object c.

Let’s take an example of Collection sort:

import java.util.*;
class collSortDemo
{
	public static void main(String[] args)
	{
		ArrayList a = new ArrayList();
		a.add("X");
		a.add("A");
		a.add("G");
		a.add("T");
		System.out.println("Before sorting : "+a);
		Collections.sort(a);
		System.out.println("After sorting : "+a);
	}
}

When we will run the above code we will get the following output:

Before sorting : [X, A, G, T]
After sorting : [A, G, T, X]

I hope this tutorial will be benefited for you.

Leave a Comment

Your email address will not be published. Required fields are marked *