All for Joomla All for Webmasters

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.   Hashtable<Integer,String> hm=new Hashtable<Integer,String>();
  5.   hm.put(100,“Amit”);
  6.   hm.put(102,“Ravi”);
  7.   hm.put(101,“Vijay”);
  8.   hm.put(103,“Rahul”);
  9.   for(Map.Entry m:hm.entrySet()){
  10.    System.out.println(m.getKey()+” “+m.getValue());
  11.   }
  12.  }
  13. }
Output:103 Rahul
       102 Ravi
       101 Vijay
       100 Amit
submit to reddit

Leave a Reply