HashTable Example

 /*  
 Hash Table doesn't allow null for both key and values.   
 we will see that in this program.   
 Author; itsafiz@gmail.com  
 */  
 import java.util.*;   
 class HashTableTest  
 {  
      public static void main(String[]args)  
      {  
      Hashtable<Integer,String> numbers = new Hashtable<Integer,String>();  
      // adding elememts   
      numbers.put(1,"one");  
      numbers.put(2,"two");  
      numbers.put(3,"three");  
      // numbers.put(4,null); // this throw NullpointerException   
      // To retrieve a value based on key   
      String num=numbers.get(3);   
      System.out.println("3 = "+num);  
      }  
 }  

Post a Comment

0 Comments