The Wayback Machine - https://web.archive.org/web/20240618020208/https://www.geeksforgeeks.org/set-contains-method-in-java-with-examples/
Open In App

Set contains() method in Java with Examples

Last Updated : 31 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The Java.util.Set.contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element.

Syntax:

boolean contains(Object element)

Parameters: The parameter element is of the type of Set. This is the element that needs to be tested if it is present in the set or not.

Return Value: The method returns true if the element is present in the set else return False.

Below program illustrate the Java.util.Set.contains() method:




// Java code to illustrate Set.contains() method
import java.io.*;
import java.util.*;
  
public class HashSetDemo {
    public static void main(String args[])
    {
        // Creating an empty Set
        Set<String> set = new HashSet<String>();
  
        // Using add() method to add elements into the Set
        set.add("Welcome");
        set.add("To");
        set.add("Geeks");
        set.add("4");
        set.add("Geeks");
  
        // Displaying the Set
        System.out.println("Set: " + set);
  
        // Check for "Geeks" in the set
        System.out.println("Does the Set contains 'Geeks'? "
                           + set.contains("Geeks"));
  
        // Check for "4" in the set
        System.out.println("Does the Set contains '4'? "
                           + set.contains("4"));
  
        // Check if the Set contains "No"
        System.out.println("Does the Set contains 'No'? "
                           + set.contains("No"));
    }
}


Output:

Set: [4, Geeks, Welcome, To]
Does the Set contains 'Geeks'? true
Does the Set contains '4'? true
Does the Set contains 'No'? false

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#contains(java.lang.Object)



Previous Article
Next Article

Similar Reads

TreeSet contains() Method in Java With Examples
TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to correctly implement the Set interface. This class pr
3 min read
Java Guava | Longs.contains() method with Examples
The Longs.contains() method of Guava's Longs Class is used to check if a value i.e. target is present in the array or not. This target value and the array are taken as parameters to this method. And this method returns a boolean value that states whether the target value is Syntax: public static boolean contains(long[] array, long target) Parameter
2 min read
LinkedHashSet contains() Method in Java with Examples
In Java, LinkedHashSet class contains methods known as contains() which is used to return true if this set contains the specified element otherwise false. Syntax: public boolean contains(Object o) Parameters: Element o as a parameter whose presence in this set is to be tested. Return Type: A boolean value, true if this set contains the specified el
2 min read
AbstractCollection contains() Method in Java with Examples
The contains() method of Java AbstractCollection is used to check whether an element is present in a Collection or not. It takes the element as a parameter and returns True if the element is present in the collection.Syntax: AbstractCollection.contains(Object element) Parameters: The parameter element is of type Collection. This parameter refers to
2 min read
ConcurrentHashMap contains() method in Java with Examples
The contains() method of Java.util.ConcurrentHashMap is used to check whether if some key maps into the specified value in this table. It is a legacy method of performing a particular task. The operation is similar to containsValue() Method of ConcurrentHashMap. Syntax: ConcurrentHashMap.contains(Object Value) Parameters: The method takes just one
2 min read
Collection contains() method in Java with Examples
The contains(Object element) of java.util.Collection interface is used to check whether the element 'element' exists in this collection. This method returns a boolean value depicting the presence of the element. If the element is present, it returns true, else it returns false. Syntax: Collection.contains(Object element) Parameters: This method acc
3 min read
List contains() method in Java with Examples
The contains() method of List interface in Java is used for checking if the specified element exists in the given list or not. Syntax: public boolean contains(Object obj) object-element to be searched for Parameters: This method accepts a single parameter obj whose presence in this list is to be tested. Return Value: It returns true if the specifie
2 min read
ConcurrentLinkedDeque contains() method in Java with Examples
The java.util.concurrent.ConcurrentLinkedDeque.contains() method is an inbuilt method in Java which checks if the specified element, passed as a parameter, is present in the deque or not. Syntax: public boolean contains(Object elem) Parameters: The method accepts a parameter elem which checks if the specified element is present in the deque or not.
2 min read
Java Guava | Booleans.contains() method with Examples
The contains() method of Booleans Class in Guava library is used to check if a specified value is present in the specified array of boolean values. The boolean value to be searched and the boolean array in which it is to be searched, are both taken as a parameter. Syntax : public static boolean contains(boolean[] array, boolean target) Parameters:
2 min read
Java Guava | Shorts.contains() method with Examples
Shorts.contains() is a method of Shorts class in Guava library which is used to check if a value target is present as an element anywhere in the array. These both target value and array are taken as parameters to this method. It returns a boolean value stating whether the target value is present or not. Syntax: public static boolean contains(short[
2 min read