Skip to content

Commit af436c4

Browse files
committed
feat(HashtableDemo): add legacy Hashtable demo with compute(), computeIfAbsent(), and Enumeration
What - Added `HashtableDemo` to demonstrate classic `java.util.Hashtable` operations. - Inserted entries (1→"A", 2→"B", 3→"C", 4→"D", 5→"E"). - Retrieved value at key 3 using raw Hashtable (cast to String). - Updated entry at key 2 with `compute()` to append `"Z"` (B → BZ). - Added entry for missing key 7 using `computeIfAbsent()` → "Z7". - Iterated over values and keys using legacy `Enumeration`. Why - To show both modern Map methods (`compute`, `computeIfAbsent`) and older iteration style (`Enumeration`) in a synchronized legacy map. - Demonstrates how Hashtable behaves with raw (non-generic) usage and type casting. - Helps illustrate why Hashtable is considered legacy compared to HashMap/ConcurrentHashMap. How - Step 1: Create raw `Hashtable` (non-generic). - Step 2: Insert five entries using `put()`. - Step 3: Call `get(3)` → returns `Object`, cast to `String`. - Step 4: Call `ht.compute(2, (k,v) -> v+"Z")` → updates value for key 2. - Step 5: Call `ht.computeIfAbsent(7, k -> "Z"+k)` → inserts missing key 7. - Step 6: Use `ht.elements()` to enumerate values. - Step 7: Use `ht.keys()` to enumerate keys. Logic - Inputs: integer keys, string values. - Outputs: - Value lookup for key 3: `"C"`. - Hashtable after recomputations: key 2 updated to `"BZ"`, new key 7 added with `"Z7"`. - Enumerations print values and keys in unspecified order. - Flow: 1. Populate hashtable. 2. Access and update entries. 3. Add new entry conditionally. 4. Iterate using legacy APIs. Real-life applications - Historical codebases where Hashtable is still in use. - Synchronized map needed for simple multi-threaded contexts (pre-Java 5). - Educational demos comparing legacy vs modern collections. Notes - Hashtable is fully synchronized (all methods), slower under contention. - Does not allow null keys or null values. - Enumeration is the pre-Iterator API; modern code should use `entrySet()` or `forEach`. - For new code, prefer `ConcurrentHashMap` for concurrency or `HashMap` for unsynchronized use. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 443a365 commit af436c4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.Enumeration;
2+
import java.util.Hashtable;
3+
4+
public class HashtableDemo {
5+
public static void main(String[] args) {
6+
// Older collection class (synchronized). Non-generic raw usage (keeps your original logic).
7+
Hashtable ht = new Hashtable();
8+
9+
// Put some entries
10+
ht.put(1, "A");
11+
ht.put(2, "B");
12+
ht.put(3, "C");
13+
ht.put(4, "D");
14+
ht.put(5, "E");
15+
16+
// get returns Object because we used raw Hashtable — cast to String to use it as string.
17+
String s = (String) ht.get(3);
18+
System.out.println("Value at key 3: " + s);
19+
20+
// Demonstrate compute: update value at key 2 by appending "Z" (B -> BZ)
21+
ht.compute(2, (k, v) -> v + "Z");
22+
23+
// computeIfAbsent: if key 7 not present, compute and put "Z7"
24+
ht.computeIfAbsent(7, (k) -> "Z" + k);
25+
26+
// Enumerations for values and keys (legacy iteration style)
27+
Enumeration e = ht.elements(); // enumeration of values
28+
Enumeration e1 = ht.keys(); // enumeration of keys
29+
30+
System.out.println("\nHashtable Values:");
31+
while (e.hasMoreElements()) {
32+
System.out.println(e.nextElement());
33+
}
34+
35+
System.out.println("\nHashtable Keys:");
36+
while (e1.hasMoreElements()) {
37+
System.out.println(e1.nextElement());
38+
}
39+
40+
/* Notes:
41+
This shows put/get, compute(), computeIfAbsent(), and enumeration over elements/keys.
42+
This file kept your original raw Hashtable usage and cast for get().
43+
Hashtable is synchronized (legacy); prefer ConcurrentHashMap / HashMap in new code.
44+
*/
45+
}
46+
}

0 commit comments

Comments
 (0)