Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

HashMap vs. Hashtable: Which map should you choose?

While the Hashtable was part of the initial Java release over two decades ago, the HashMap is the correct key-value store to use today.

When Java 2 was released, the architects of the language completely redesigned the collection classes. A thorough overhaul introduced a variety of new abstract classes, interfaces and components, including the properly camel-cased HashMap. This class was intended to replace the Hashtable. Let's explore the similarities and differences between the two and settle the HashMap vs. Hashtable debate once and for all.

HashMap vs. Hashtable differences

One key HashMap vs. Hashtable difference is the fact that the older component is synchronized, which means concurrent access to the Hashtable’s underlying collection isn't allowed. Method locks limit access to one thread at a time. On the surface, this sounds like a benefit, but it’s not.

A drawback to synchronization is that it’s costly in terms of performance. However, it's unlikely that the performance difference between a HashMap and Hashtable will ever be of any consequence for a traditional enterprise application. Network latency and database connections typically act as a much larger performance bottleneck than JVM speed. The bigger issue with class level synchronization is that it promotes lazy development practices.

A concurrent application shouldn't rely on the underlying collection class to mitigate synchronized access to its content. Instead, the application should write its own synchronized methods to coordinate threads. If the application controls the synchronization, the underlying Hashtable or HashMap won't need to. The best practice is to synchronize only when needed. The HashMap promotes this programming paradigm while the Hashtable performs synchronization even when it's not necessary.

How to synchronize a HashMap

What if a developer needs constant, low-level synchronization at the class level? It's still not a justification to choose a Hashtable. A developer can create a synchronized HashMap through the following method call:

package com.mcnz.example;

import java.util.HashMap;
import java.util.Hashtable;

/* Code to compare Hashtable and HashMap */
public class HashMapvsHashtableExample {

  // Difference between Hashtable and HashMap example
  public static void main(String[] args) {
        
    // Java Hashtable example
    Hashtable<String,String> hashTableExample = 
                      new Hashtable<String, String>();
    hashTableExample.put("ford", "Mustang SUV");
    hashTableExample.put("dodge", "Viper ACR");
    hashTableExample.put("chevy", "Chevette");
        
    System.out.println("Hashtable example code:" 
                                  + hashTableExample);
 
        // Java HashMap example
    HashMap<String,String> hashMapExample = 
                       new HashMap<String,String>();
    hashMapExample.put("ford", "Mustang SUV");
    hashMapExample.put("dodge", "Viper ACR");
    hashMapExample.put("chevy", null);
    hashMapExample.put(null, "Corvette");
    System.out.println("HashMap example code :"+hashMapExample);
  }
}

Furthermore, the Java API provides a special class called the ConcurrentHashMap which offers synchronization facilities over and above both that of the HashMap and Hashtable. The special class provides methods such as computeIfAbsent and putIfAbsent which implement the 'check and put' synchronization design pattern.

Hashtable vs. HashMap: How these Java map implementations differ
Name Hashtable HasMap
CamelCase No Yes
Introduced JDK 1.0 JDK 1.2
Parent class Dictionary AbstractMap
Subclasses LinkedHashMap, PrinterStateReasons Properties, UIDefaults
Synchronized Yes No
Single null key allowed No Yes
Values can be null No Yes
Status Obsolete but not deprecated Active
Recommended No Yes

HashMap vs. Hashtable similarities

Both the Hashtable and HashMap implement the Map interface and both share the same set of methods used to add, remove and manipulate elements of a key-value, pair-based collection class. As this example code shows, HashMap and Hashtable are programmatically similar. It can also be found on GitHub.

Which Java map should a developer choose?

There really isn't any debate about HashMap vs. Hashtable anymore. The Hashtable served developers well in Java's infancy, but all new applications should choose the HashMap as their preferred key-value collection class.

View All Videos
App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close