BACK
This tutorial explains how to check whether Hashset is empty using boolean isEmpty() method.
Returns True if this set contains no elements.
To check hashset whether empty or not and return result in Boolean value.
Example
import java.util.HashSet; public class CheckEmpty { public static void main(String a[]) { HashSet<Integer> hs = new HashSet<Integer>(); hs.add(12); hs.add(23); hs.add(5); hs.add(6); hs.add(6); System.out.println(hs); System.out.println("Is HashSet empty? "+hs.isEmpty()); hs.remove("third"); System.out.println(hs); } }
Output
[5, 6, 23, 12]
Is HashSet empty? false
[5, 6, 23, 12]