java.util.HashSet.isEmpty() Method

Advertisements


Description

The isEmpty() method is used to check if this set contains no elements.

Declaration

Following is the declaration for java.util.HashSet.isEmpty() method.

public boolean isEmpty()

Parameters

  • NA

Return Value

The method call returns 'true' if this set contains no elements.

Exception

  • NA

Example

The following example shows the usage of java.util.HashSet.isEmpty()

package com.tutorialspoint;

import java.util.*;

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

Let us compile and run the above program, this will produce the following result.

Is the hash set empty: false



Advertisements
Advertisements