CodeToConcept
About

java

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

JavaHTMLCSS

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

Java Hashtable

Hashtable is a legacy class in Java that stores data in the form of key-value pairs.

It implements the Map interface.

Key -> Value

For example:

101 -> Ashish
102 -> Rahul
103 -> Priya

Hashtable belongs to the java.util package.

import java.util.Hashtable;

Why was Hashtable used?

Hashtable was introduced in early versions of Java for storing key-value pairs.

It provides synchronized methods, which means its basic operations are designed for synchronized access.

However, for most modern applications, Hashtable is usually not the first choice. Depending on the requirement, developers commonly use:

  • HashMap for general-purpose non-concurrent use.
  • ConcurrentHashMap for concurrent access.
  • LinkedHashMap when predictable iteration order is required.
  • TreeMap when sorted keys are required.

Creating a Hashtable

Hashtable<Integer, String> table =
    new Hashtable<>();

Adding Elements

Use put().

table.put(101, "Ashish");
table.put(102, "Rahul");
table.put(103, "Priya");

Retrieving a Value

Use get().

System.out.println(table.get(101));

Output

Ashish

Duplicate Keys

A Hashtable does not allow duplicate keys.

table.put(1, "Java");
table.put(1, "Python");

The second value replaces the first value.

Final mapping:

1 -> Python

Duplicate Values

Duplicate values are allowed.

table.put(1, "Java");
table.put(2, "Java");

This is valid.

Null Keys and Values

Unlike HashMap, Hashtable does not allow null keys or null values.

For example:

table.put(null, "Java");

This results in an exception.

Similarly:

table.put(1, null);

is not allowed.

This is an important difference between Hashtable and HashMap.

Hashtable Does Not Guarantee Sorted Order

Hashtable does not provide sorted key ordering.

It also should not be used when insertion order needs to be preserved.

If order matters, consider LinkedHashMap.

If sorted keys are required, consider TreeMap.

Iterating Through Hashtable

You can iterate through its entries using entrySet():

for (Map.Entry<Integer, String> entry :
        table.entrySet()) {
 
    System.out.println(
        entry.getKey() + " -> " + entry.getValue()
    );
}

Important Hashtable Methods

Hashtable provides common Map methods such as:

MethodPurpose
put()Adds or updates an entry
get()Retrieves a value
remove()Removes an entry
containsKey()Checks for a key
containsValue()Checks for a value
size()Returns number of entries
isEmpty()Checks whether it is empty
clear()Removes all entries
keySet()Returns keys
values()Returns values
entrySet()Returns key-value entries

Example

import java.util.Hashtable;
 
public class HelloWorld {
    public static void main(String[] args) {
 
        Hashtable<Integer, String> students =
            new Hashtable<>();
 
        students.put(101, "Ashish");
        students.put(102, "Rahul");
        students.put(103, "Priya");
 
        System.out.println(students.get(102));
    }
}

Output

Rahul

Hashtable and Synchronization

One historical characteristic of Hashtable is that its methods are synchronized.

This means its operations provide synchronized access at the method level.

However, synchronized methods do not automatically make every multi-step operation in an application atomic.

For modern concurrent applications, ConcurrentHashMap is generally a more appropriate choice when concurrent map access is required.

Hashtable vs HashMap

HashtableHashMap
Legacy classModern general-purpose Map
Synchronized methodsNot synchronized
Does not allow null keysAllows one null key
Does not allow null valuesAllows multiple null values
No guaranteed orderingNo guaranteed ordering
Usually avoided in new code unless legacy compatibility requires itCommonly used for general-purpose Map operations

Hashtable vs ConcurrentHashMap

HashtableConcurrentHashMap
Legacy synchronized MapDesigned for concurrent applications
Synchronizes methodsDesigned for better concurrent access
Does not allow null keys/valuesDoes not allow null keys/values
Older designModern concurrent collection

Hashtable vs LinkedHashMap

HashtableLinkedHashMap
Synchronized methodsNot synchronized
No guaranteed insertion orderMaintains insertion order by default
Does not allow null keys/valuesAllows one null key and multiple null values
Legacy collectionModern Map implementation

Important Points

  • Hashtable implements Map.
  • It stores key-value pairs.
  • Keys are unique.
  • Duplicate values are allowed.
  • It does not allow null keys.
  • It does not allow null values.
  • Its methods are synchronized.
  • It does not guarantee insertion or sorted order.
  • It is a legacy collection class.
  • ConcurrentHashMap is generally preferred for modern concurrent applications.

Key Takeaway

Hashtable is an older synchronized Map implementation.

For new Java applications, choose the Map implementation based on the actual requirement:

General key-value storage
        ↓
HashMap
 
Need insertion order
        ↓
LinkedHashMap

Need sorted keys ↓ TreeMap

Need concurrent access ↓ ConcurrentHashMap

Understanding Hashtable is still important because it is part of the Java Collections Framework and is commonly encountered in older Java codebases.

See you in next page....🙂

← TreeMapIterator →
← TreeMapIterator →