Java Collection Tutorial - Java HashSet.contains(Object o)








Syntax

HashSet.contains(Object o) has the following syntax.

public boolean contains(Object o)

Example

In the following code shows how to use HashSet.contains(Object o) method.

/* w w w .j a  v a  2  s. c  om*/

import java.util.HashSet;

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

The code above generates the following result.