Thursday, 17 April 2014

HashSet : contains : Search for an Element

public boolean contains(Object o)
Returns true if this set contains the specified element.

import java.util.*;

class HashSetContains{
 public static void main(String args[]){
  HashSet<Integer> mySet = new HashSet<> ();
  
  /* Add Elements to HashSet */
  mySet.add(10);
  mySet.add(20);
  mySet.add(30);
  mySet.add(40);
  
  System.out.println("Elements in HashSet are");
  System.out.println(mySet);
  
  System.out.print("\nIs mySet contains 10 ");
  System.out.println(mySet.contains(10));
  
  System.out.print("\nIs mySet contains 90 ");
  System.out.println(mySet.contains(90));
  
 } 
}

Output
Elements in HashSet are
[20, 40, 10, 30]

Is mySet contains 10 true

Is mySet contains 90 false



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment