Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Overview on NavigableSet subtype of Java Collections

This tutorial is focuses on the subtype “NavigableSet” and a brief introduction to the “Map Interface” subtype with more attention towards several attributes of NavigableSet.

The java.util.NavigableSet is nothing but an interface that is subtype of the java.util.SortedSet http://tutorials.jenkov.com/java-collections/set.html interface. The behaviour of the same is much similar on the lines of a SortedSet.

The only exception here is that, we have methods of navigation available other than the sorted mechanisms that are available in the Sortedset. Let us take a closer look at these navigation methods.

We will be going through the following topics in this tutorial

Here is a list of the topics covered in this text:

  1. descendingIterator() and descendingSet()
  2. headSet(), tailSet() and subSet()
  3. ceiling(), floor(), higher() and lower()
  4. pollFirst(),pollLast()

descendingIterator() and descendingSet()

Here are some of the navigation methods,the descendingIterator() and descendingSet()methods.

The objective of the descendingSet() is to fetch a NavigableSet thereby reversing the sequence or order of the elements compared to this one. Alterations to the set in the descending order are reflected in the original set of elements as well for the reason that the returned “view” has a back up of original NavigableSet.

Here is a simple example:

NavigableSet reverse = original.descendingSet();

The task of the descendingIterator() method is to permit the iteration of elements in a reverse order or the sequence for the set NavigableSet, also known as the SortedSet and this is done with no need of altering the sequencing of elements.

Iterator reverse = original.descendingIterator(); 

headSet(), tailSet() and subSet()

The headSet() method returns a view of the original NavigableSet which only contains elements that are "less than" the given element. Here is an example:

NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
//this headset will contain "1" and "2"
SortedSet headset = original.headSet("3");
//this headset will contain "1", "2", and "3" for the reason that "inclusive"=true
NavigableSet headset = original.headSet("3", true);

The tailSet() method also functions in the same manner. The only exception is that it gives back all the elements that are placed at a higher level when compared to the assigned parameter element.

The subSet()also permit the developers to make a way for two parameters thereby separating the edges or boundaries of the view set to return. The elements that are matching as far as the first boundary is concerned are included. On the other hand, the ones that match the last boundary are not included.

Here is an example:

NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
original.add("4");
original.add("5");
//this subset will contain "2" and "3"
SortedSet    subset  = original.subSet("2", "4");
//this subset will contain "2", "3" and "4" since
//    fromInclusive=true, and toInclusive=true 
NavigableSet subset = original.subSet("2", true, "4", true);

ceiling(), floor(), higher(), and lower()

The function of the ceiling() method plays the role to fetch the least or the smallest element in this set. This is nothing but greater than or equal to the element passed in the form parameter to the ceiling() method. Here is an example:

NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
//ceiling will be "2".
Object ceiling = original.ceiling("2");
//floor will be "2".
Object floor = original.floor("2");

Talking about the floor() method, it operates just the opposite of ceiling().

The higher() method task is to extract or return the least or smallest element in this set. This is greater than and not equal tos the element that is passed in the form of parameter to the higher() method. Here is an example:

NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
//higher will be "3".
Object higher = original.higher("2");

//lower will be "1"
Object lower = original.lower("2");

On the other hand, the lower() method role is just the opposite of the higher() method.

pollFirst() and pollLast()

The pollFirst() method normally extracts and then deletes the "first" element in the NavigableSet or null in the situation when the set is empty.

Talking about the pollLast(), it acts to fetch and then remove the element tat is placed “last” in the set. And in case there is no element in the set, the value “null” is returned. The term “First’ points to the smallest element as per the set sort’s order where on, on the other hand, last refers to the largest as per the element sorting sequence of the set.

Here are two examples:

NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");


//first is "1"
Object first = original.pollFirst();

//last is "3"
Object last = original.pollLast();

The tutorial will give a brief introduction on Map Interface of Java and discuss the same in more detail in the upcoming articles.

The java.util.Map is an interface that defines a mapping between a key and a value. Point to note here is that the Map interface is definitely not a subtype of the Collection interface thereby making it to behave a bit different from other types of collection.

The list of topics that covers our next tutorial would comprise of the following.

  1. Map Implementations
  2. Adding and Accessing Elements
  3. Removing Elements
  4. Generic Maps
  5. More Details in the JavaDoc

Map Implementations

Now, for the reason that Map is an interface, one would require to instantiate a functionality of the interface that is concrete so that the same can be used effectively. One has the liberty to selecting from the subsequent Map implementations that are present in the java Collections API. you need to instantiate a concrete implementation of the interface in order to use it.

  • java.util.HashMap
  • java.util.Hashtable
  • java.util.EnumMap
  • java.util.IdentityHashMap
  • java.util.LinkedHashMap
  • java.util.Properties
  • java.util.TreeMap
  • java.util.WeakHashMap

Example using NavigableSet Subtype:

Here is the code of program:

import java.util.*;
import java.util.concurrent.*;

public class NavigableSetExample{
  public static void main(String[] args) {
  System.out.println("Navigable set Example!\n");
  NavigableSet <Integer>nSet = new ConcurrentSkipListSet<Integer>();
  nSet.add(10);
  nSet.add(20);
  nSet.add(50);
  nSet.add(30);
  nSet.add(100);
  nSet.add(80);
  // Returns an iterator over the elements in navigable set,
    in ascending order.
  Iterator iterator = nSet.iterator();
  System.out.print("Ascending order navigable set: ");
  //Ascending order list
  while (iterator.hasNext()){
  System.out.print(iterator.next() + " ");
  }
  System.out.println();
  //Descending order list
  System.out.println("Descending order navigable set: " + 
   nSet.descendingSet() + "\n");
  //Greater than or equal to the given element
  System.out.println("Least element in Navigable set greater than 
   or equal to 35: " + nSet.ceiling(35));
  //Less than or equal to the given element
  System.out.println("Greatest element in Navigable set less than 
   or equal to 35: " + nSet.floor(35) + "\n");
  //Viewing the portion of navigable set whose elements are 
  strictly less than the given element
  System.out.println("Navigable set whose elements are strictly 
   less than '40': " + nSet.headSet(40));
  //Viewing the portion of navigable set whose elements are 
  greater than or equal to the given element
  System.out.println("Navigable set whose elements are greater 
  than or equal to '40': " + nSet.tailSet(40) + "\n");
  //Removing first element from navigable set
  System.out.println("Remove element: "+nSet.pollFirst());
  //After removing the first element, now get navigable set
  System.out.println("Now navigable set: " + nSet.descendingSet() + "\n");
  //Removing last element from navigable set
  System.out.println("Remove element: " + nSet.pollLast());
  //After removing the last element, now get navigable set
  System.out.println("Now navigable set: " + nSet.descendingSet());
  }
}

Program’s Output:

Programs Output


I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login