Java Program to Check Whether a HashSet Is Empty or Not

You are currently viewing Java Program to Check Whether a HashSet Is Empty or Not

Java program to check whether a HashSet is empty or not

// Java program to check whether a HashSet 
// is empty or not

import java.util.*;

public class Main {
  public static void main(String[] args) {
    HashSet < Integer > nums = new HashSet();
    HashSet < Integer > nums1 = new HashSet();

    nums.add(1);
    nums.add(2);
    nums.add(3);
    nums.add(4);

    if (nums.isEmpty())
      System.out.println("The HashSet nums is an empty collection.");
    else
      System.out.println("The HashSet nums is not an empty collection.");

    if (nums1.isEmpty())
      System.out.println("The HashSet nums1 is an empty collection.");
    else
      System.out.println("The HashSet nums1 is not an empty collection.");
  }
}

Output

The HashSet nums is not an empty collection.
The HashSet nums1 is an empty collection.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply