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-
[ 0 ] Comments