Let’s learn HashSet isEmpty() method in java.
HashSet isEmpty() method in java
isEmpty() method of HashSet class returns true if this set contains no elements.
Syntax:
public boolean isEmpty()
Parameters:
isEmpty() method do not take any parameter.
Now let’s see example on HashSet isEmpty() method.
import java.util.HashSet; public class HashSetIsEmptyMethodExample { public static void main(String[] args) { HashSet<String> hs = new HashSet<String>(); hs.add("Welcome"); hs.add("hello"); hs.add("world"); hs.add("core"); hs.add("java"); System.out.println("HashSet before using isEmpty() method: " + hs); // check if HashSet is empty System.out.println("Is the HashSet empty: " + hs.isEmpty()); // clear HashSet using clear() method hs.clear(); // again check if HashSet is empty System.out.println("Is the HashSet empty: " + hs.isEmpty()); } }
Output:
HashSet before using isEmpty() method: [core, world, java, Welcome, hello]
Is the HashSet empty: false
Is the HashSet empty: true
Also read – major features of java