TreeSet class

 

see Collection interface  for descriptions and examples of most of  TreeSet's methods

 

■ Like HashSet, TreeSet extends the AbstractSet class.  But it also implements the SortedSet interface.  It is for creating an automatically ordered set, with no duplicates, in ascending order. Like all Sets, TreeSet allows just one null element.

 

TreeSet is for sorted collections. In general, when you use TreeSet instead of ArrayList, you are giving up your ability to have duplicate elements in return for having your items naturally sorted. You also will have somewhat fewer methods with which to work.

 

TreeSet adds five methods to the familiar Collection methods.  They are: .first( ),  .headSet(...), .last( ),  .subSet(...),  .tailSet(...). 

 

 

Object  .first( )  method

 

■ Returns first/last element or NoSuchElementException if set is empty.

 

 

Object  .last( )  method

 

■ Returns first/last element or NoSuchElementException if set is empty.

 

 

SortedSet   .headSet( Obj )  method

 

■ Returns a (live) SortedSet view of all elements less/higher than the Obj, exclusive of Obj

 

 

SortedSet   .tailSet( Obj )   method

 

■ Returns a (live) SortedSet view of all elements less/higher than the Obj, exclusive of Obj

 

 

SortedSet  .subSet( fromObj, toObj )  method

 

■ Returns a (live) SortedSet view of all elements inclusive of the first Obj, to the last one exclusive of the final Obj