Working of Hashtable

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hi, this article will deal with working of Hashtable in Java. We all know that Hashtable is a data structure which is used to store key and value pairs of object in java. It is not mandatory for us to specify the value of Hashtable initially.

Working of Hashtable:

Working of Hashtable depends on various parameters. Initial capacity, load factor, size and Threshold value are the parameters which affect Hashtable performance.

  • Initial Capacity:  This is the capacity of Hashtableto store number of key value pairs when it is instantiated. Default capacity is 16
  • Load Factor: A parameter responsible to determine when to increase size of Hashtable. Default load factor is 0.75
  • Size : number of key value pairs in Hashtable.
  • Threshold value: When number of key value pairs is more than threshold value, then Hashtable is resized. Threshold value is the product of initial capacity and load factor. so default will become 0.75 * 16 = 12.

Here is what official javadoc says about Hashtable.

This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value.To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.

Hashtable  is quite similar to HashMap except few differences

  • Hashtable methods are synchronized while HashMap methods are not synchronized.
  • Hashtable is slower whereas HashMap is faster because it does not have to deal with synchronization.
  • Hashtable is a fail-fast collection while HashMap is fail-safe.
  • Hashtable does not allow any null key or null value whereas HashMap allows one null key and many null values.

Working of Hashtable put and get is greatly similar to that of HashMap put method and HashMap get method.

I hope this article helped understand working of Hashtable.

Share Button

1 comment for “Working of Hashtable

Leave a Reply

Your email address will not be published. Required fields are marked *