AdSense

AdSense3

Saturday, 12 September 2015

Java Hashtable class

  • A Hashtable is an array of list.Each list is known as a bucket.The position of bucket is identified by calling the hashcode() method.A Hashtable contains values based on the key. It implements the Map interface and extends Dictionary class.
  • It contains only unique elements.
  • It may have not have any null key or value.
  • It is synchronized.

Example of Hashtable:

  1. import java.util.*;  
  2. class TestCollection16{  
  3.  public static void main(String args[]){  
  4.    
  5.   Hashtable<Integer,String> hm=new Hashtable<Integer,String>();  
  6.   
  7.   hm.put(100,"Amit");  
  8.   hm.put(102,"Ravi");  
  9.   hm.put(101,"Vijay");  
  10.   hm.put(103,"Rahul");  
  11.   
  12.   for(Map.Entry m:hm.entrySet()){  
  13.    System.out.println(m.getKey()+" "+m.getValue());  
  14.   }  
  15.  }  
  16. }  
Output:103 Rahul
       102 Ravi
       101 Vijay
       100 Amit

No comments:

Post a Comment