Monday, 17 October 2016

java.util.TreeSet class

java.util.TreeSet class

java.util.TreeSet class
  • We can store group of objects in TreeSet object.
  • Here group of objects means, it can allow us to store same type of objects only.
    • Same type means, assuming that all objects are in the form of integer types.
    • If we are trying to insert different type of objects we will get run time exception as ClassCastException  
  • TreeSet will display the output in ascending order.
  • Its is introduced in 1.2 version.
  • Size will increase dynamically.
--------------------------------------------------------------------------------------------
Program         :       Example on TreeSet class
Program name  :       TSDemo1.java
Output            :         
      [1, 2, 3, 5, 6, 7]
     
import java.util.TreeSet;

class TSDemo1
{
       public static void main(String args[])
       {
             TreeSet treeSet= new TreeSet();
              
             treeSet.add(7);
             treeSet.add(1);
             treeSet.add(6);
             treeSet.add(2);
             treeSet.add(5);
             treeSet.add(3);
             
             System.out.println(treeSet);
       }
}

Compile       :         javac TSDemo1.java
Run             :         java TSDemo1
Output        :         
     [1, 2, 3, 5, 6, 7]
--------------------------------------------------------------------------------------------
Thanks for your time.
Nireekshan