Friday 6 June 2014

TreeMap : containsKey (Object key) : Search for a key in HashMap

public boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key.

import java.util.*;

class TreeMapContainsKey{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;

  myMap = new TreeMap<> ();
  
  /* Add Elements to myMap2 */
  myMap.put(10, "c");
  myMap.put(1, "a");
  myMap.put(15, "d");
  myMap.put(5, "b");
  myMap.put(99, "e");

  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.print("Is myMap contains key 10 ");
  System.out.println(myMap.containsKey(10));
  System.out.print("Is myMap contains key 100 ");
  System.out.println(myMap.containsKey(100));
 }
}

Output
Elements in myMap are
{1=a, 5=b, 10=c, 15=d, 99=e}
Is myMap contains key 10 true
Is myMap contains key 100 false

1. Throws ClassCastException if the specified key cannot be compared with the keys currently in the map

import java.util.*;

class TreeMapContainsKeyClassCast{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;

  myMap = new TreeMap<> ();
  
  /* Add Elements to myMap2 */
  myMap.put(10, "c");
  myMap.put(1, "a");
  myMap.put(15, "d");
  myMap.put(5, "b");
  myMap.put(99, "e");

  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.print("Is myMap contains key \"abc\" ");
  System.out.println(myMap.containsKey("abc"));
 }
}

Output
Elements in myMap are
{1=a, 5=b, 10=c, 15=d, 99=e}
Is myMap contains key "abc" Exception in thread "main" java.lang.ClassCastExcept
ion: java.lang.Integer cannot be cast to java.lang.String
        at java.lang.String.compareTo(String.java:111)
        at java.util.TreeMap.getEntry(TreeMap.java:352)
        at java.util.TreeMap.containsKey(TreeMap.java:232)
        at TreeMapContainsKeyClassCast.main(TreeMapContainsKeyClassCast.java:20)

2. Throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys.

import java.util.*;

class TreeMapContainsKeyNullPointer{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;

  myMap = new TreeMap<> ();
  
  /* Add Elements to myMap2 */
  myMap.put(10, "c");
  myMap.put(1, "a");
  myMap.put(15, "d");
  myMap.put(5, "b");
  myMap.put(99, "e");

  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.print("Is myMap contains key null ");
  System.out.println(myMap.containsKey(null));
 }
}

Output
Elements in myMap are
{1=a, 5=b, 10=c, 15=d, 99=e}
Is myMap contains key null Exception in thread "main" java.lang.NullPointerExcep
tion
        at java.util.TreeMap.getEntry(TreeMap.java:347)
        at java.util.TreeMap.containsKey(TreeMap.java:232)
        at TreeMapContainsKeyNullPointer.main(TreeMapContainsKeyNullPointer.java
:20)




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment