Gmail - HashTable
Gmail - HashTable
1 message
Hashtable is a part of the Java Collections Framework but is considered somewhat outdated compared to more modern
collections like HashMap. It is a collection of key/value pairs where both keys and values are objects.
How it Works:
Internal Structure: Hashtable internally uses a hash table (array of linked lists). The keys are hashed, and the hash codes determine
the bucket in which the key-value pair will be placed.
Synchronization: Unlike HashMap, Hashtable is synchronized, making it thread-safe. This means that only one thread can access a
Hashtable at a time, which can lead to performance issues in multi-threaded environments if synchronization is not needed.
Null Keys/Values: Hashtable does not allow null keys or null values. In contrast, HashMap allows one null key and multiple null
values.
---
Return Type: V (previous value associated with the key, or null if no mapping existed).
Example:
2. get(Object key)
Return Type: V (the value associated with the key, or null if not found).
Example:
3. remove(Object key)
Description: Removes the key-value pair from the Hashtable based on the given key.
Return Type: V (the value that was associated with the key, or null if no mapping existed).
Example:
Example:
5. containsValue(Object value)
Example:
6. size()
Example:
System.out.println(table.size()); // Output: 0
7. clear()
Example:
8. keys()
Example:
9. values()
Example:
Collection<Integer> values = table.values();
for (Integer val : values) {
System.out.println(val);
}
Description: Copies all key-value pairs from another map (or Hashtable) into the current Hashtable.
Example:
---
1. Create a Hashtable:
2. Update a Value:
To update the value associated with an existing key, you simply use the put() method, which replaces the old value for that key.
3. Retrieve a Value:
To retrieve the value associated with a specific key, use the get() method.
Important Note: Hashtable does not support direct sorting as it is based on hashing. If you need to sort the keys or values, you can
convert the Hashtable to a List or a TreeMap (which is inherently sorted).
Sorting by Keys:
To sort by values, convert the Hashtable to a List of Map.Entry objects, then sort it:
---
Program Example
import java.util.*;
// Retrieve value
System.out.println("Value of Banana: " + table.get("Banana"));
// Update value
table.put("Apple", 60);
System.out.println("Updated table: " + table);
// Remove an entry
table.remove("Mango");
System.out.println("After removal of Mango: " + table);
// Sorting by values
List<Map.Entry<String, Integer>> list = new ArrayList<>(table.entrySet());
list.sort(Map.Entry.comparingByValue());
System.out.println("Sorted by values: " + list);
}
}
Output:
Value of Banana: 20
Updated table: {Apple=60, Banana=20, Mango=30}
After removal of Mango: {Apple=60, Banana=20}
Sorted by keys: {Apple=60, Banana=20}
Sorted by values: [(Banana=20), (Apple=60)]
---
Summary:
Hashtable is a legacy collection class that is synchronized and thread-safe but has performance overhead due to synchronization.
The methods provided allow for insertion, retrieval, updating, deletion, and basic iteration over key-value pairs.
It is generally recommended to use HashMap instead of Hashtable in modern applications, unless synchronization is specifically
required.