Saturday 19 April 2014

TreeSet : clear() : Remove all elements from TreeSet

public void clear()
Removes all of the elements from this set.

import java.util.*;
class TreeSetClear{
 public static void main(String args[]){
  TreeSet<Integer> mySet = new TreeSet<> ();
  
  /* Add Elements to the set */
  mySet.add(100);
  mySet.add(10);
  mySet.add(120);
  mySet.add(21);
  mySet.add(1);
  mySet.add(-5);
  
  System.out.println("Elements in the set are");
  System.out.println(mySet);
 
  System.out.println("\nAbout to call clear method ");
  
  mySet.clear();
  
  System.out.println("\nElements in the set are");
  System.out.println(mySet);
 }
}

Output
Elements in the set are
[-5, 1, 10, 21, 100, 120]

About to call clear method

Elements in the set are
[]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment