HashMap vs HashTable

Working with Java and storing key-value pairs, but confused about choosing between HashMap and HashTable? 

Don’t worry; this article will help you better understand the differences between HashMap and HashTable. Both are fundamental data structures which use hash-based storage for key-value pairs. 

However, they cater to different needs. They provide functionalities like the capability to work on multi-threaded applications and the fastest way to retrieve data. Understanding the difference between the two is essential to know the specific usage of HashMap and HashTable.

What is a HashMap?

A HashMap in Java is a widely used data structure known for its implementation in the Map interface. Ideal for single-threaded applications where synchronization is handled externally. It provides constant time performance for most operations like insertion and retrieval. It stores data in key-value pairs and is known for its speedy results.

Imagine a library which has approximately 70 categories of books. Each category contains approximately 200 books and has a barcode. 

Instead of searching category shelves endlessly, we can scan the barcode and track the book’s location. That’s precisely how a HashMap works. It maps keys, in our case, the barcodes to values and, in our case, the location of books.

Features of HashMap:

  • Allow null keys and values: in the HashMap, you can have a single null key and multiple null values.
  • Not synchronized: it is efficient for single-threaded applications and does not handle multiple-threaded applications simultaneously. 
  • High performance: in environments where synchronization is not required, it has high performance and fast speed.

Syntax

HashMap<K, V> map = new HashMap<>();

Where K = type of keys and V= type of values.

Example

import java.util.HashMap;

public class HashMapExample {

public static void main(String[] args) {

// Create a HashMap

HashMap<Integer, String> map = new HashMap<>();

// Add key-value pairs

map.put(1, "Apple");

map.put(2, "Banana");

map.put(null, "Cherry"); // Allows one null key

map.put(3, null);       // Allows multiple null values

// Print the HashMap

System.out.println("HashMap: " + map);

}

}

The output of the above code:

HashMap: {null=Cherry, 1=Apple, 2=Banana, 3=null}

Explanation

The example demonstrates the use of a HashMap in Java, which stores key-value pairs. Here’s a brief explanation:

1. Creation: A `HashMap` object is created.

2. Adding Entries: Key-value pairs are added using `put()`. 

  • Key `1` maps to “Apple”.
  • Key `2` maps to “Banana”.
  • `null` is used as a key for “Cherry” (HashMap allows one `null` key).
  • Key `3` maps to `null` (HashMap allows multiple `null` values).

3. Output: The `HashMap` contents are printed using `System.out.println`.  

Key Points:

  • HashMap allows one `null` key and multiple `null` values.
  • The order of elements in a HashMap is not guaranteed.

Output:  

  • HashMap: {null=Cherry, 1=Apple, 2=Banana, 3=null}`

Need a detailed understanding of HashMaps?
Have more insights from HashMap IN JAVA – Everything You Need to Know About!

What is a HashTable?