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 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

Prasad Kharkar

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.

7 thoughts on “Working of Hashtable

  • Pingback:Working of Collections.synchronizedMap - theJavaGeek

  • July 21, 2017 at 6:31 pm
    Permalink

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

    Reply
    • July 24, 2017 at 1:10 pm
      Permalink

      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 🙂

      Reply
      • August 19, 2017 at 6:08 pm
        Permalink

        So default capacity of hsahmap is 11 according to you

        Reply
        • August 19, 2017 at 10:09 pm
          Permalink

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

          Reply
  • May 22, 2018 at 4:44 am
    Permalink

    Brother Hasmap is also Fail Fast concurrent hashmap is fail safe.

    Reply
    • May 22, 2018 at 9:35 am
      Permalink

      Thanks brother for correction. I have made the correction. 🙂 Keep providing constructive feedback.

      Reply

Leave a Reply

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