The SortedSet

The SortedSet


Posted in : Core Java Posted on : October 20, 2010 at 4:35 PM Comments : [ 0 ]

This section contains the detail about the SortedSet in java.

The SortedSet

The Sorted Set  inherits the behavior of Set by extending it and sort the set in increasing order. If there is no element in invoking Set, 'NoSuchElementException' is thrown by many methods. A 'ClassCastException' is thrown when an object is incompatible with the elements in a set. A 'NullPointerException' is thrown if an attempt is made to use a null object and null is not allowed in the set.

   Methods     Description  
Comparator comparator( ) Returns the invoking sorted set's comparator. If the
 natural ordering is used for this set, null is returned.
Object first( )  Returns the first element in the invoking sorted set.
SortedSet headSet(Object end) Returns a SortedSet containing those elements less
than end that are contained in the invoking sorted set.
Elements in the returned sorted set are also referenced
by the invoking sorted set.
Object last( ) Returns the last element in the invoking sorted set.
SortedSet subSet(Object start, Object end) Returns a SortedSet that includes those elements
between start and end.1. Elements in the returned
collection are also referenced by the invoking object.
SortedSet tailSet(Object start) Returns a SortedSet that contains those elements
greater than or equal to start that are contained in
the sorted set. Elements in the returned set are also
referenced by the invoking object.

Example :


import java.util.*;

public class SortedSetExample {
	
	public static void main(String[] args) {

		// Create the sorted set
		SortedSet SS = new TreeSet();

		// Add elements to the set
		SS.add("Y");
		SS.add("X");
		SS.add("Z");

		// Iterating over the elements in the set
		Iterator IT = SS.iterator();
		while (IT.hasNext()) {
			// Get element
			Object element = IT.next();
			System.out.println(element.toString());
		}
	}

}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>java SortedSetExample
X
Y
Z

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics