Saturday 19 April 2014

TreeSet : contains : Search for Element in TreeSet

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

import java.util.*;

class TreeSetContains{
 public static void main(String args[]){
  TreeSet<Integer> mySet = new TreeSet<> ();
  
  /* Add Elements to TreeSet */
  mySet.add(10);
  mySet.add(30);
  mySet.add(40);
  mySet.add(20);
  
  System.out.println("Elements in TreeSet 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 TreeSet are
[10, 20, 30, 40]

Is mySet contains 10 true

Is mySet contains 90 false

1. throws ClassCastException if the specified object cannot be compared
with the elements currently in the set

import java.util.*;

class TreeSetContainsClassCast{
 public static void main(String args[]){
  TreeSet<Integer> mySet = new TreeSet<> ();
  
  /* Add Elements to TreeSet */
  mySet.add(10);
  mySet.add(30);
  mySet.add(40);
  mySet.add(20);
  
  System.out.println("Elements in TreeSet are");
  System.out.println(mySet);
  
  System.out.println("\nTrying to search for a real value in Integer set");
  System.out.println(mySet.contains(10.01));  
 } 
}

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

Trying to search for a real value in Integer set
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer canno
t be cast to java.lang.Double
        at java.lang.Double.compareTo(Double.java:49)
        at java.util.TreeMap.getEntry(TreeMap.java:346)
        at java.util.TreeMap.containsKey(TreeMap.java:227)
        at java.util.TreeSet.contains(TreeSet.java:234)
        at TreeSetContainsClassCast.main(TreeSetContainsClassCast.java:17)


2. throws NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

import java.util.*;

class TreeSetContainsNullPointer{
 public static void main(String args[]){
  TreeSet<Integer> mySet = new TreeSet<> ();
  
  /* Add Elements to TreeSet */
  mySet.add(10);
  mySet.add(30);
  mySet.add(40);
  mySet.add(20);
  
  System.out.println("Elements in TreeSet are");
  System.out.println(mySet);
  
  System.out.println("\nTrying to search for a null value in Integer set");
  System.out.println(mySet.contains(null));  
 } 
}

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

Trying to search for a null value in Integer set
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.getEntry(TreeMap.java:342)
        at java.util.TreeMap.containsKey(TreeMap.java:227)
        at java.util.TreeSet.contains(TreeSet.java:234)
        at TreeSetContainsNullPointer.main(TreeSetContainsNullPointer.java:17)





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment