HashSet contains(Object) in Java

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

Syntax:

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

This method takes one argument. This method returns true if this set contains the specified element.

Parameters: One parameter is required for this method.

o: element whose presence in this set is to be tested.

Returns: true if this set contains the specified element.

Exceptions: NA

Approach

Java

import java.util.HashSet;

public class HashSetcontains {
    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";

        System.out.println(hashSet.contains(o));

    }
}

Output:

true


No comments:

Post a Comment