java.util.HashSet.contains() Method

Advertisements


Description

The contains(Object o) method is used to return 'true' if this set contains the specified element.

Declaration

Following is the declaration for java.util.HashSet.contains() method.

public boolean contains(Object o)

Parameters

  • o--This is the element whose presence in this set is to be tested.

Return Value

The method call returns 'true' if this set contains the specified element.

Exception

  • NA

Example

The following example shows the usage of java.util.HashSet.contains()

package com.tutorialspoint;

import java.util.*;

public class HashSetDemo {
   public static void main(String args[]) {
      // create hash set
      HashSet <String> newset = new HashSet <String>();
            
      // populate hash set
      newset.add("Learning"); 
      newset.add("Easy");
      newset.add("Simply");  
      
      // check the existence of element
      boolean exist=newset.contains("Simply");
         
      System.out.println("Is the element 'Simply' exists: "+ exist);
   }    
}

Let us compile and run the above program, this will produce the following result.

Is the element 'Simply' exists: true



Advertisements
Advertisements