The Treeset class
When iterated TreeSet guarantees the order of the elements, but the guaranteed order is the sorting order of the elements. The order in which the elements would be sorted or arranged, if we used a Collections.sort() on a List or array hold these elements. If they implement Comparable then this sorted order is determined either by their normal order (), or by a specific implementation of Comparator.
- Treeset provides an implementation of the set interface that uses a tree for storage.
- Objects are stored in sorted, ascending order.
- Access and retrieval times are quote fast, which makes tree, set an excellent choice when storing large amounts of sorted information that must be found quickly.
The following are the constructors defined in this class
In this constructs an empty tree set that will be sorted in ascending order according to the natural order of its elements.
Treeset (collection c):
Here builds a tree set that contains the elements of c.
Tree set (comparator comp):
In this tree set constructs an empty tree set that will be sorted according to the comparator specified by comp.
Treeset (sorted set Ss):
Here builds a tree set that contains the elements of Ss.
The following program demonstrates a tree set
Public static void main (string args )
Tree set ts = new tree set ();
ts. add (“c”);
ts. add (“A”);
ts. add (“B”);
ts. add (“E”);
ts. add (“F”);
ts. add (“D”);
ts. add (“ts”);
system .out. println ( ts);
- The output of the above program will be A B C D E F
- As explained, because tree set stores its element in a tree, they are automatically arranged in sorted order as the output conforms.
The Treeset Class Homework Help
Tutorspoint.com java experts provide best java homework solutions at an affordable price to American students. Get Help on any Java Projects