HashSet remove(Object) in Java

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

Syntax:

boolean java.util.HashSet.remove(Object o)

This method takes one argument. This method removes the specified element from this set if it is present.

Parameters: One parameter is required for this method.

o: object to be removed from this set, if present.

Returns: true if the set contained the specified element.

Exceptions: NA

Approach

Java

import java.util.HashSet;

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

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

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

        Object o = "Hello";
        hashSet.remove(o);

        System.out.println(hashSet);
    }
}

Output:

[Java, C++]


No comments:

Post a Comment