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 11
  • 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. T

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

5 comments for “Working of Hashtable

  1. sangam
    July 21, 2017 at 6:31 pm

    Empty Hashtable is a default initial capacity 11. plz check.

    • July 24, 2017 at 1:10 pm

      hi Sangam,
      thanks for pointing out. Maybe I was having HashMap in my mind while writing or Hashtable 🙂 That is why I made mistake. Happy learning 🙂

      • Faisal
        August 19, 2017 at 6:08 pm

        So default capacity of hsahmap is 11 according to you

        • August 19, 2017 at 10:09 pm

          No Hashmap capacity is 16. I had written it for Hashtable also which I corrected.

Leave a Reply

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