Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

JAVA HashTable

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

HashtableDemo.java:
import java.util.*;
classHashtableDemo
{
	public static void main(String args[])
	{
		Hashtablehm=new Hashtable();
		hm.put(100,"Tapuuu");
		hm.put(102,"Silan");
		hm.put(101,"Alok");
		hm.put(103,"Tilan");
		for(Map.Entry m:hm.entrySet())
		{
			System.out.println(m.getKey()+" "+m.getValue());
		}
	}
}

Output

103 Tilan
102 Silan
101 Alok
100 Tapuuu

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

Summary :

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.

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext