Commit dfb58bd
committed
feat(HashtableEnumerationDemo): add demo of legacy Enumeration iteration with Hashtable
What
- Added `HashtableEnumerationDemo` to showcase iterating over a `Hashtable` using `Enumeration`.
- Populated table with entries: (1→"A"), (2→"BZ"), (3→"C"), (7→"Z7").
- Enumerated values with `ht.elements()`.
- Enumerated keys with `ht.keys()`.
- Printed results sequentially.
Why
- To demonstrate the legacy `Enumeration` API still supported by `Hashtable`.
- Highlights the difference from modern iteration (`Iterator`, `forEach`).
- Educational example of synchronized legacy collections and how they were iterated pre-Java 2.
How
- Step 1: Create generic `Hashtable<Integer,String>`.
- Step 2: Insert four entries.
- Step 3: Call `ht.elements()` to retrieve `Enumeration<String>` of values.
- Step 4: Iterate using `while(valuesEnum.hasMoreElements())`.
- Step 5: Call `ht.keys()` to retrieve `Enumeration<Integer>` of keys.
- Step 6: Iterate and print each key.
Logic
- Inputs: hard-coded key–value pairs.
- Outputs:
- Console logs values: `"A"`, `"BZ"`, `"C"`, `"Z7"` (order not guaranteed).
- Console logs keys: `1, 2, 3, 7` (order not guaranteed).
- Flow:
1. Insert entries.
2. Enumerate values.
3. Enumerate keys.
- Enumeration is forward-only, does not support remove or fail-fast semantics.
Real-life applications
- Maintaining or reading legacy codebases where `Hashtable` + `Enumeration` are used.
- Simple demos showing historical evolution from `Enumeration` to `Iterator`/`Stream`.
- Rarely used in production; modern APIs offer more functionality and safety.
Notes
- `Hashtable` is synchronized (all methods thread-safe).
- `Enumeration` is not fail-fast: concurrent modifications do not throw `ConcurrentModificationException`.
- Prefer `forEach`, `entrySet().iterator()`, or streams in new code.
- Demonstrates backward compatibility in Java Collections Framework.
Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>1 parent af436c4 commit dfb58bd
1 file changed
Lines changed: 38 additions & 0 deletions
File tree
- Section 25 Collections Frameworks/Map Interface/Hash Table/src
Lines changed: 38 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 | + | |
0 commit comments