Friday, 18 April 2014

HashSet : remove : Remove an element from HashSet

public boolean remove(Object o)
Removes the specified element from this set if it is present. Return true if the set contained the specified element.

import java.util.*;

class HashSetRemove{
 public static void main(String args[]){
  HashSet<Integer> mySet = new HashSet<> ();
  
  /* Add Elements to mySet */
  mySet.add(10);
  mySet.add(20);
  mySet.add(30);
  mySet.add(40);
  
  System.out.println("Elements in mySet are");
  System.out.println(mySet);
  
  System.out.print("\nIs 30 removed ");
  System.out.println(mySet.remove(30));
  
  System.out.println("\nElements in mySet are");
  System.out.println(mySet);
  
  System.out.print("\nIs 90 removed ");
  System.out.println(mySet.remove(90));
  
  System.out.println("\nElements in mySet are");
  System.out.println(mySet);
 } 
}

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

Is 30 removed true

Elements in mySet are
[20, 40, 10]

Is 90 removed false

Elements in mySet are
[20, 40, 10]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment