Thursday 17 April 2014

HashSet : isEmpty() : Check whether HashSet is empty or not

public boolean isEmpty()
Returns true if this set contains no elements.

import java.util.*;

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

Output
Is mySet empty true

Adding elements to mySet

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

Is mySet empty false


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment