Check if a particular element exists in HashSet - Java Collection Framework

Java examples for Collection Framework:HashSet

Description

Check if a particular element exists in HashSet

Demo Code


import java.util.HashSet;
 
public class Main {
 
  public static void main(String[] args) {
    HashSet hSet = new HashSet();
   /*from www  .  j ava  2s.c om*/
    hSet.add(new Integer("1"));
    hSet.add(new Integer("2"));
    hSet.add(new Integer("3"));
 
    boolean blnExists = hSet.contains(new Integer("3"));
    System.out.println("3 exists in HashSet ? : " + blnExists);
 
  }
}

Result


Related Tutorials