1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* Check if a particular element exists in Java HashSet Example This Java Example shows how to check whether an elements is contained in Java HashSet using contains method. */ import java.util.HashSet; public class CheckElementHashSetExample { public static void main(String[] args) { //create object of HashSet HashSet hSet = new HashSet(); //add elements to HashSet object hSet.add(new Integer("1")); hSet.add(new Integer("2")); hSet.add(new Integer("3")); /* To check whether a particular value exists in HashSet use boolean contains(Object value) method of HashSet class. It returns true if the HashSet contains the value, otherwise false. */ boolean blnExists = hSet.contains(new Integer("3")); System.out.println("3 exists in HashSet ? : " + blnExists); } } /* Output would be 3 exists in HashSet ? : true */ |
Add Comment