Commit af436c4
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- Section 25 Collections Frameworks/Map Interface/Hash Table/src
1 file changed
+46
-0
lines changedLines changed: 46 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
0 commit comments