- It implements the Map interface and extends Dictionary class.
- To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.
- An instance of Hashtable has two parameters that affect its performance: initial capacity and load factor(0.75). The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. Note that the hash table is open: in the case of a "hash collision", a single bucket stores multiple entries, which must be searched sequentially. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
- If many entries are to be made into a Hashtable, creating it with a sufficiently large capacity may allow the e ntries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.
- It is synchronized.
Let's see a program for better understanding:
public static void main(String args)
Difference between HashMap and Hashtable:
HashMap and Hashtable both implement java.util.Map interface but there are some differences that Java developers must understand to write more efficient code.
- One of the major differences between HashMap and Hashtable is that HashMap is non-synchronized whereas Hashtable is synchronized, which means Hashtable is thread-safe and can be shared between multiple threads but HashMap cannot be shared between multiple threads without proper synchronization.
- The HashMap class is roughly equivalent to Hashtable, except that it permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn't allow nulls).
- One more notable difference between Hashtable and HashMap is that because of thread-safety and synchronization Hashtable is much slower than HashMap if used in Single threaded environment. So if you don't need synchronization and HashMap is only used by one thread, it out performHashtable in Java.
- HashMap does not guarantee that the order of the map will remain constant over time.
Note that HashMap can be synchronized by
Map m = Collections.synchronizedMap(hashMap);
From the interview point of view Collection Framework chapter is fifty percent and the remaining chapters are fifty percent according to my point of view. So this chapter is very important according to the real-time scenario. In this chapter I have given a depth concept about Collection and its implementation classes by taking some demo programs. I also presented Map interface and its implementation classes by taking simple examples. I hope this chapter will make a good platform.
Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects
SILAN Technology, BBSR Call to: 0674-2361252