Java HashSet isEmpty() Method Tutorial

In this section, we will learn what the HashSet isEmpty() method is and how to use it in Java.

What is Java HashSet isEmpty() Method?

The Java HashSet isEmpty() method is used to check whether a HashSet object is empty.

Java HashSet isEmpty() Method Syntax:

public boolean isEmpty()

isEmpty() Method Parameters:

The method does not take an argument.

isEmpty() Method Return Value:

The return value of this method is of type Boolean.

If the target HashSet object was empty, the return value of this method is true otherwise the value false will return.

isEmpty() Method Exceptions:

The method does not throw an exception.

Example: using HashSet isEmpty() method

import java.util.HashSet; 
public class Main {

    public static void main(String[] args){
      HashSet<Integer> set = new HashSet<>();
      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);
      set.add(5);
      set.add(6);
      set.add(7);

      System.out.println(set.isEmpty());
    }
}

Output:

false

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies