🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The Hashtable class in Java is a part of the java.util package.
It is a legacy class that implements a hash table, which maps keys to values.
It is synchronized and can be used in multithreaded environments.
Table of Contents
- What is the
HashtableClass? - Common Methods
- Examples of Using the
HashtableClass - Conclusion
1. What is the Hashtable Class?
The Hashtable class implements a hash table data structure, which allows you to store and retrieve key-value pairs. It is synchronized, meaning it is thread-safe and can be shared among multiple threads without additional synchronization.
2. Common Methods
put(K key, V value): Maps the specified key to the specified value.get(Object key): Returns the value to which the specified key is mapped.remove(Object key): Removes the key (and its corresponding value) from the hashtable.containsKey(Object key): Returnstrueif the hashtable contains the specified key.containsValue(Object value): Returnstrueif the hashtable maps one or more keys to the specified value.size(): Returns the number of key-value mappings in the hashtable.isEmpty(): Returnstrueif the hashtable contains no key-value mappings.keys(): Returns an enumeration of the keys in the hashtable.elements(): Returns an enumeration of the values in the hashtable.clear(): Clears the hashtable so that it contains no key-value mappings.
3. Examples of Using the Hashtable Class
Example 1: Creating and Using a Hashtable
This example demonstrates how to create and use a Hashtable with key-value pairs.
import java.util.Hashtable;
public class HashtableExample {
public static void main(String[] args) {
Hashtable<String, Integer> hashtable = new Hashtable<>();
// Adding elements
hashtable.put("Apple", 1);
hashtable.put("Banana", 2);
hashtable.put("Cherry", 3);
// Retrieving elements
System.out.println("Value for 'Apple': " + hashtable.get("Apple"));
// Checking if key exists
System.out.println("Contains key 'Banana': " + hashtable.containsKey("Banana"));
// Checking if value exists
System.out.println("Contains value 3: " + hashtable.containsValue(3));
// Removing an element
hashtable.remove("Banana");
System.out.println("After removing 'Banana': " + hashtable);
// Getting the size of the hashtable
System.out.println("Size of hashtable: " + hashtable.size());
}
}
Output:
Value for 'Apple': 1
Contains key 'Banana': true
Contains value 3: true
After removing 'Banana': {Cherry=3, Apple=1}
Size of hashtable: 2
Example 2: Iterating Over Keys and Values
This example shows how to iterate over the keys and values in a Hashtable.
import java.util.Enumeration;
import java.util.Hashtable;
public class HashtableIterationExample {
public static void main(String[] args) {
Hashtable<String, Integer> hashtable = new Hashtable<>();
hashtable.put("Apple", 1);
hashtable.put("Banana", 2);
hashtable.put("Cherry", 3);
// Iterating over keys
Enumeration<String> keys = hashtable.keys();
System.out.println("Keys:");
while (keys.hasMoreElements()) {
System.out.println(keys.nextElement());
}
// Iterating over values
Enumeration<Integer> values = hashtable.elements();
System.out.println("Values:");
while (values.hasMoreElements()) {
System.out.println(values.nextElement());
}
}
}
Output:
Keys:
Cherry
Apple
Banana
Values:
3
1
2
Example 3: Clearing the Hashtable
This example demonstrates how to clear all key-value mappings from a Hashtable.
import java.util.Hashtable;
public class HashtableClearExample {
public static void main(String[] args) {
Hashtable<String, Integer> hashtable = new Hashtable<>();
hashtable.put("Apple", 1);
hashtable.put("Banana", 2);
hashtable.put("Cherry", 3);
System.out.println("Hashtable before clear: " + hashtable);
// Clearing the hashtable
hashtable.clear();
System.out.println("Hashtable after clear: " + hashtable);
System.out.println("Is hashtable empty? " + hashtable.isEmpty());
}
}
Output:
Hashtable before clear: {Cherry=3, Apple=1, Banana=2}
Hashtable after clear: {}
Is hashtable empty? true
4. Conclusion
The Hashtable class in Java provides a synchronized key-value mapping, making it suitable for use in multithreaded environments. Although it has largely been replaced by the HashMap class due to its better performance in non-synchronized contexts, Hashtable is still useful when thread safety is required without additional synchronization. The examples provided demonstrate common usage patterns and highlight the capabilities of the Hashtable class.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment