Get Size of Java TreeSet Example


/*
  Get Size of Java TreeSet Example
  This Java Example shows how to get the size or nubmer of elements stored in
  Java TreeSet object using size method.
*/

import java.util.TreeSet;

public class GetSizeOfJavaTreeSetExample{

 
public static void main(String[] args) {
   
   
//create TreeSet object
   
TreeSet tSet = new TreeSet();
   
   
/*
      To get the size of TreeSet use
      int size() method of TreeSet class. It returns the number of elements
      stored in TreeSet object.     
    */
   
System.out.println("Size of TreeSet : " + tSet.size());
   
   
//add elements to TreeSet object
   
tSet.add(new Integer("1"));
    tSet.add
(new Integer("2"));
    tSet.add
(new Integer("3"));

    System.out.println
("Size of TreeSet after addition : " + tSet.size());
   
   
//remove one element from TreeSet using remove method
   
tSet.remove(new Integer("1"));
    System.out.println
("Size of TreeSet after removal : " + tSet.size());
 
}
}

/*
Output would be
Size of TreeSet : 0
Size of TreeSet after addition : 3
Size of TreeSet after removal : 2
*/ 



Related Examples

Simple Java TreeSet example

Remove specified element from Java TreeSet example

Remove all elements from Java TreeSet example

Iterate through elements of Java TreeSet example

Get Tail Set from Java TreeSet example

Get Sub Set from Java TreeSet example

Get lowest and highest value stored in Java TreeSet example

Get Head Set from Java TreeSet example

Copy all elements of Java TreeSet to an Object Array Example

Check if a particular value exists in Java TreeSet example