Java Collection Framework - SortedSet Interface

Java Collection Framework - SortedSet Interface


Posted in : Java Posted on : December 4, 2010 at 6:18 PM Comments : [ 0 ]

SortedSet interface is part of java.util package. SortedSet interface can add value add(value) elements.

Java Collection Framework - SortedSet Interface

SortedSet interface is part of java.util package. SortedSet interface can add value add(value) elements. Value of SortedSet can get by Iterator Interface.The java.util.SortedSet interface is a subtype of the java.util.Set interface. It behaves like a normal set with the exception that the elements are sorted internally. This means that when you iterate the elements of a SortedSet the elements are returned in the sorted order.

The order of the sorting is either the natural sorting order of the elements or the order determined by a Comparator that you can give to the SortedSet.By default the behavior of a set sorted in ascending order.
The Java Collections API only has one implementation of the SortedSet interface - the java.util.TreeSet class. 

In addition to those methods defined by Set, the SortedSet interface declares the methods summarized as below.

comparator( ) - This method returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.

first( ) - This method returns the first (lowest) element currently in this set.

headSet(E toElement) - This method returns a view of the portion of this set whose elements are strictly less than toElement.

last( ) - This method returns the last (highest) element currently in this set.

subSet(E fromElement, E toElement) - This method returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

tailSet(E fromElement) - This method returns a view of the portion of this set whose elements are greater than or equal to fromElement.

 Here is the example to show how to implement sortedSet.

Example:-SortedSetDemo,java

package devmanuals.com;

import java.util.*;

public class SortedSetDemo {
	public static void main(String args[]) {
		SortedSet Sset = new TreeSet();
		Sset.add("Gyan");
		Sset.add("Ankit");
		Sset.add("Arun");
		Sset.add("Anand");
		Sset.add("Ram");
		System.out.println("The SortedSet entries is as follows : " + Sset);
	}
}
Output:-
The SortedSet entries is as follows : 

[Anand, Ankit, Arun, Gyan, Ram]


Download This Code.

Here is the Example of methods implementation of SortedSet class.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics