Thursday 17 April 2014

HashSet : clear : Clear the data in HashSet

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

import java.util.*;

class HashSetClear{
 public static void main(String args[]){
  HashSet<Integer> mySet = new HashSet<> ();
  
  /* Add Elements to HashSet */
  mySet.add(10);
  mySet.add(20);
  mySet.add(30);
  mySet.add(40);
  
  System.out.println("Elements in HashSet are");
  System.out.println(mySet);
  
  System.out.println("\nCalling clear method on Map");
  
  mySet.clear();
  
  System.out.println("Elements in HashSet after clearing");
  System.out.println(mySet);
 } 
}

Output
Elements in HashSet are
[20, 40, 10, 30]

Calling clear method on Map
Elements in HashSet after clearing
[]

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment