Wednesday 19 March 2014

Search For An Element in the Set

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

import java.util.*;
class SetSearch{
 public static void main(String args[]){
  Set<Integer> mySet = new TreeSet<> ();
  
  /* Add Elements to the mySet */
  mySet.add(10);
  mySet.add(20);
  mySet.add(30);
  
  System.out.print("Elements in the Set are ");
  System.out.println(mySet);
  System.out.println("Is set contains 10 " + mySet.contains(10));
  System.out.println("Is set contains 100 " + mySet.contains(100));
 }
}

 Output

Elements in the Set are[10, 20, 30]
Is set contains 10 true
Is set contains 100 false


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment