• September 1, 2014
  • Core Java
  • Comments Off on SortedSet interface in java

SortedSet interface in java

SortedSet interface:

SortedSet interface extends Set interface. SortedSet interface maintain ascending order in its elements.

 Note: SortedSet interface declares its own methods in addition of methods declared in Set interface.

 Commonly used methods of SortedSet interface:

1. comparator(): Returns comparator for this sorted set. It returns null if the natural ordering is used for this set.

Syntax: public Comparator comparator().

2. first(): Returns the first element in this sorted set.

Syntax: public Object first().

3. headSet(Object end): Returns a SortedSet containing those elements less than end that are contained in this sorted set.

Syntax: public SortedSet headSet(Object end).

4. last(): Returns the last element in this sorted set.

Syntax: public Object last().

5. subSet(Object start, Object end): Returns a SortedSet containing those elements between start and end -1.

Syntax: public SortedSet subSet(Object start, Object end).

6. tailSet(Object start): Returns a SortedSet that contains those elements greater than or equal to start that are contained in this sorted set.

Syntax: public SortedSet tailSet(Object start).

 A simple example of TreeSet class to explain few methods of SortedSet interface.

TreeSetTest.java

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
 
/**
 * This class is used to show the TreeSet functionality.
 * @author javawithease
 */
public class TreeSetTest {
	public static void main(String args[]){
		//Create TreeSet object.
		Set treeSet = new TreeSet();
 
		//Add objects to the HashSet.
		treeSet.add("Roxy");
		treeSet.add("Sunil");
		treeSet.add("Sandy");
		treeSet.add("Munish");
		treeSet.add("Pardeep");
 
		//Print the TreeSet object.
		System.out.println("TreeSet elements:");
		System.out.println(treeSet);
 
		//Print the TreeSet elements using iterator.
		Iterator iterator=treeSet.iterator();
		System.out.println("TreeSet elements using iterator:");
		while(iterator.hasNext()){  
		   System.out.println(iterator.next());  
		}  
	}
}

Output:

TreeSet elements:
[Munish, Pardeep, Roxy, Sandy, Sunil]
TreeSet elements using iterator:
Munish
Pardeep
Roxy
Sandy
Sunil

Download this example.
 
Next Topic: List interface in java with example.
Previous Topic: Set interface in java with example.

Related Topics:

Collection framework in java.
Collection interfaces in java.
Collection interface in java with example.
Set interface in java with example.
SortedSet interface in java with example.
List interface in java with example.
Map interface in java with example.
Daemon thread in java in java with example.
SortedMap interface in java with example.
Queue interface in java with example.
Deque interface in java with example.
Enumeration interface in java with example.
Collection classes in java with example.
HashSet in java with example.
LinkedHashSet in java with example.
TreeSet in java with example.
ArrayList in java with example.
LinkedList in java with example.
HashMap in java with example.
LinkedHashMap in java with example.
TreeMap in java with example.
PriorityQueue in java with example.
ArrayDeque in java with example.
Abstract classes in collection framework in java with example.
Sorting in java with example.
Comparable interface in java with example.
Comparator interface in java with example.
Properties class in java with example.
Hashtable in java with example.
ListIterator interface in java with example.

 


Comments are closed.