Java Collections Framework- TreeSet Class

Java Collections Framework- TreeSet Class


Posted in : Java Posted on : November 24, 2010 at 5:06 PM Comments : [ 0 ]

This class implements the Set interface, The TreeSet implementations useful when you need to extract elements from a collection in a sorted manner.? 

Java Collections Framework- TreeSet Class

This class implements the Set interface, The TreeSet implementations useful when you need to extract elements from a collection in a sorted manner. 
TreeSet stores objects in a sorted manner. TreeSet stores its elements in a tree and they are automatically arranged in a sorted order. TreeSet is not synchronized. If more than one thread wants to access it at the same time then it must be synchronized externally.

The TreeSet class supports four constructors. The first form constructs an empty tree set that will be sorted in ascending order according to the natural order of its elements:

TreeSet( )
TreeSet(Collection c)
TreeSet(Comparator comp)
TreeSet(SortedSet ss)

Here is an example that demonstrates a TreeSet: TreeSetDemo.java

package devmanuals.com;

import java.util.*;

public class TreeSetDemo {
	public static void main(String args[]) {
		// Create a tree set
		TreeSet oTreeSet = new TreeSet();
		// Add elements to the tree set
		oTreeSet.add("C");
		oTreeSet.add("A");
		oTreeSet.add("B");
		oTreeSet.add("E");
		oTreeSet.add("F");
		oTreeSet.add("D");
		System.out.println(oTreeSet);
	}
}

Output:

[A, B, C, D, E, F]

Here is the More example of methods of the TreeSet class-

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics