HashSet clear() in Java

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

Syntax:

void java.util.HashSet.clear()

This method removes all of the elements from this set.

Note: The set will be empty after this call returns.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

import java.util.HashSet;

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

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

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

        hashSet.clear();

        System.out.println(hashSet);

    }
}

Output:

[]


No comments:

Post a Comment