TreeSet

 TreeSet

  • TreeSet internally follows tree structure.
  • TreeSet is not a synchronized class.
  • TreeSet is the implementation class of the Set interface which is also used to store groups of individual objects but duplicate values are not allowed.
  • TreeSet is called an unordered Collection because it is not guaranteed for insertion order of elements but all elements are stored in sorted order(by default ascending order).
  • TreeSet supports only one null value if TreeSet is Empty otherwise TreeSet does not support null values because it internally performs comparison operations but we never compare a null value. with any object and it will throw a RuntimeException saying NullPointerException.
  • TreeSet does not allow to store of different types of objects because it internally performs the comparison. operations but we never compare 2 different types of it will throw a RuntimeException saying ClassCastException.


Creation of TreeSet

TreeSet<E> ts=new TreeSet<E>();

TreeSet<E> ts=new TreeSet<E>(SortedSet);

TreeSet<E> ts=new TreeSet<E>(Comparator);

TreeSet<E> ts = new TreeSet<E>(Collection obj);


Example:

TreeSetExample
Output:

TreeSetOutput


Difference between  HashSet vs LinkedHashSet vs TreeSet.



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.