Thursday, 11 June 2015

How to Search key in Hashtable Java

import java.util.Hashtable;

public class HashTableSearchKey {
   
    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
         /*
          * Returns: true if and only if the specified object
          * is a key in this hashtable, as determined by the equals method;
             false otherwise.
          */
        System.out.println(" key =1   ="+ht.containsKey(1));
        System.out.println(" key =10   ="+ht.containsKey(10));
        System.out.println(" key =5   ="+ht.containsKey(5));
  }   

}





Output:


key =1   =true

key =10   =false

key =5   =true

No comments:

Post a Comment