Trusted answers to developer questions

HashMap vs. HashTable in Java

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

HashMaps and HashTables are both classes in Java which use the hash table data structure to store data as key-value pairs. Although they are quite similar, there are some differences between them.

Differences

HashMap

  • Launched in JDK 1.2

  • Fail-fast[1]

  • Allows one null as a key and multiple nulls as values


  • Manual synchronisation[3] required in case of multiple threads

HashTable

  • Launched in JDK 1.0

  • Fail-safe

  • Nulls aren’t allowed


  • Uses Enumeration[2] or Iterator

  • Automatically synchronised



Explanation

  1. When an error occurs, if a system exists and does not proceed further, then it is considered to be fail-fast. Otherwise, it is called fail-safe. Since a HashMap’s Iterator is fail-fast, our program will terminate if an exception is thrown. (Note: A fail-fast Iterator throws an exception if we try to modify our data while traversal).

  2. Enumeration and Iterator are pretty much the same; both are used for traversal. An Iterator can also delete an element using remove(), whereas an Enumeration cannot manipulate the class it traverses. Also, Enumeration is a legacy interface (can only traverse legacy classes like Vector, etc.).

  3. Thread safety means that our data remains consistent when viewed or edited by two or more threads. When one thread accesses it for editing, it will be inaccessible by others. This can be done by passing your HashMap to a built-in function in the Java Collections Framework which returns a synchronised (thread-safe) map:

    HashMap<int, int> syncMap = Collections.synchronizedMap(myMap);


Hierarchy

Class Diagram for HashMap and HashTable
Class Diagram for HashMap and HashTable

Example

The following example demonstrates the difference of storing null keys and values in a HashMap and a HashTable. (An exception will be thrown if you try to insert null as a key or value; try uncommenting line 22 and 23)

import java.util.*;
public class Test {
public static void main(String[] argv){
Map<Integer, String> map = new HashMap<>();
map.put(null, "value with null key");
map.put(1, "123");
map.put(2, "abc");
map.put(3, null);
System.out.println("HashMap's output:");
System.out.println(map.get(null));
System.out.println(map.get(1));
System.out.println(map.get(2));
System.out.println(map.get(3));
Hashtable<Integer, String> table = new Hashtable<>();
// Try uncommenting the following lines.
// table.put(null, "xyz");
// table.put(3, null);
table.put(1, "cat");
table.put(2, "dog");
System.out.println("\nHashTable's output:");
System.out.println(table.get(1));
System.out.println(table.get(2));
}
}

RELATED TAGS

hashmap
hashtable
hash
java
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?