Latest posts by Prasad Kharkar (see all)
- CRUD application using angular 4 spring rest services spring data jpa - June 16, 2017
- Full stack application using angular 4 and spring boot - June 14, 2017
- RESTful web services using spring boot - April 19, 2017
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.
123 public class <span class="typeNameLabel">Hashtable<K,V></span>extends <a title="class in java.util" href="https://docs.oracle.com/javase/8/docs/api/java/util/Dictionary.html">Dictionary</a><K,V>implements <a title="interface in java.util" href="https://docs.oracle.com/javase/8/docs/api/java/util/Map.html">Map</a><K,V>, <a title="interface in java.lang" href="https://docs.oracle.com/javase/8/docs/api/java/lang/Cloneable.html">Cloneable</a>, <a title="interface in java.io" href="https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html">Serializable</a>This class implements a hash table, which maps keys to values. Any non-
nullobject 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
hashCodemethod and the
- 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.
I hope this article helped understand working of Hashtable.