0% found this document useful (0 votes)
30 views4 pages

Java Hashtable: Features & Usage Guide

Hashtable in Java is a synchronized data structure that stores key-value pairs, does not allow null keys or values, and is thread-safe. It is slightly slower than HashMap due to its synchronization. Common operations include adding, accessing, checking, and removing elements, and it is recommended for legacy applications where thread safety is required.

Uploaded by

ravigupta230290
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Java Hashtable: Features & Usage Guide

Hashtable in Java is a synchronized data structure that stores key-value pairs, does not allow null keys or values, and is thread-safe. It is slightly slower than HashMap due to its synchronization. Common operations include adding, accessing, checking, and removing elements, and it is recommended for legacy applications where thread safety is required.

Uploaded by

ravigupta230290
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Hashtable in Java

1. Introduction
A Hashtable in Java is a part of the [Link] package. It is an implementation of a hash table
data structure that stores data in key-value pairs.

- Keys are unique.


- Values can be duplicate.
- It does not allow null keys or null values.
- It is synchronized, meaning it is thread-safe but slightly slower compared to HashMap.

2. Declaration and Initialization

import [Link];

public class Main {


public static void main(String[] args) {
// Creating a Hashtable
Hashtable<Integer, String> table = new Hashtable<>();

// Adding key-value pairs


[Link](1, "Java");
[Link](2, "Python");
[Link](3, "C++");

// Printing the Hashtable


[Link](table);
}
}

Output:
{3=C++, 2=Python, 1=Java}

3. Important Features
1. Thread-Safe → All methods are synchronized.
2. No null key/value → Inserting null results in NullPointerException.
3. Order not guaranteed → Elements are not stored in insertion order.
4. Performance → Slightly slower than HashMap due to synchronization.

Skillio, Pune +91-9130502135 | +91-8484831616


4. Common Operations

4.1 Adding Elements


[Link](4, "JavaScript");

4.2 Accessing Elements


[Link]([Link](2)); // Output: Python

4.3 Checking if a Key/Value Exists

[Link]([Link](3)); // true
[Link]([Link]("C++")); // true

4.4 Removing Elements


[Link](1); // Removes key 1 and its value

4.5 Iterating Through Hashtable

for (Integer key : [Link]()) {


[Link](key + " -> " + [Link](key));
}

Output:
3 -> C++
2 -> Python
4 -> JavaScript

Skillio, Pune +91-9130502135 | +91-8484831616


5. Hashtable vs HashMap
Feature Hashtable HashMap

Thread-Safe Yes (synchronized) No

Null Keys Not allowed One allowed

Null Values Not allowed Multiple allowed

Performance Slower Faster

Introduced JDK 1.0 JDK 1.2

6. Example: Hashtable in Real Use

import [Link];

public class LoginSystem {


public static void main(String[] args) {
Hashtable<String, String> users = new Hashtable<>();

// Adding users
[Link]("avinash", "password123");
[Link]("rahul", "hello@123");

// Validating login
String username = "avinash";
String password = "password123";

if([Link](username) && [Link](username).equals(password)) {


[Link]("✅ Login Successful!");
} else {
[Link]("❌ Invalid Credentials!");
}
}
}

Output:
✅ Login Successful!

Skillio, Pune +91-9130502135 | +91-8484831616


7. When to Use Hashtable?
- When you need a thread-safe map.
- When null keys/values are not required.
- For legacy applications (though ConcurrentHashMap is preferred now).

Summary
Hashtable is synchronized, does not allow nulls, and ensures thread safety. For modern
applications, HashMap or ConcurrentHashMap is often a better choice.

Skillio, Pune +91-9130502135 | +91-8484831616

You might also like