0% found this document useful (0 votes)
2 views

TreeSet+in+Java

TreeSet is a sorted Set implementation that does not allow duplicate or null values and does not preserve insertion order. It is based on a self-balancing binary search tree and provides various methods for adding, clearing, and accessing elements. Constructors allow for creating an empty TreeSet or converting an existing Collection to a TreeSet.

Uploaded by

abhimanyu thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

TreeSet+in+Java

TreeSet is a sorted Set implementation that does not allow duplicate or null values and does not preserve insertion order. It is based on a self-balancing binary search tree and provides various methods for adding, clearing, and accessing elements. Constructors allow for creating an empty TreeSet or converting an existing Collection to a TreeSet.

Uploaded by

abhimanyu thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Collection

: TreeSet
Collection Framework:
TreeSet
• TreeSet implements the sorted Set.
• Duplicate and Null Values are not allowed in
TreeSet.
• TreeSet does not preserve the insertion order.
•TreeSet is basically implementation of a self-
balancing binary search tree.
Constructors in TreeSet
TreeSet t = new TreeSet();
This will create empty TreeSet object in which elements
will get stored in default natural sorting order.

TreeSet t = new TreeSet(Collection col);


This constructor is used when we want to convert any
Collection object to TreeSet object.
Methods in : TreeSet
void add(): This method will add specified element according
to some sorting order in TreeSet. Duplicate entires will not
get added.
boolean addAll(): This method will add all elements of
specified Collection to the set.
void clear() : This method will remove all the elements.
boolean contains(): This method will return true if given
element is present in TreeSet else it will return false.
Methods in : TreeSet
Object first() : This method will return first element in TreeSet.
Object last(): This method will return last element in TreeSet.
SortedSet headSet(): This method will return elements of TreeSet
which are less than the specified element.
SortedSet tailSet(): This method will return elements of TreeSet
which are greater than or equal to the specified element.
SortedSet subSet(Object fromElement, Object toElement): This
method will return elements ranging from fromElement to
toElement.

You might also like