Java Collection Tutorial - Java HashSet.isEmpty()








Syntax

HashSet.isEmpty() has the following syntax.

public boolean isEmpty()

Example

In the following code shows how to use HashSet.isEmpty() method.

/*from   w w w  . j a v  a 2  s.c  o  m*/

import java.util.HashSet;

public class Main {
   public static void main(String args[]) {
      HashSet<String>  newset = new HashSet <String> ();
                  
      // populate hash set
      newset.add("Learning"); 
      newset.add("from");
      newset.add("java2s.com");  
      
      // check if the has set is empty
      boolean isempty=newset.isEmpty();
         
      System.out.println("Is the hash set empty: "+ isempty);
   }    
}

The code above generates the following result.