HashSet isEmpty() in Java

isEmpty(): This method is available in java.util.HashSet class of Java.

Syntax:

boolean java.util.HashSet.isEmpty()

This method returns true if this set contains no elements.

Parameters: NA

Returns: true if this set contains no elements.

Exceptions: NA

Approach

Java

import java.util.HashSet;

public class HashSetisEmpty {
    public static void main(String[] args) {

        HashSet<String> hashSet = new HashSet<String>();

        hashSet.add("Hello");
        hashSet.add("Java");
        hashSet.add("C++");
        hashSet.add("Hello");

        System.out.println(hashSet.isEmpty());

    }
}

Output:

false


No comments:

Post a Comment