Skip to content

Commit dfb58bd

Browse files
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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Enumeration;
2+
import java.util.Hashtable;
3+
4+
public class HashtableEnumerationDemo {
5+
public static void main(String[] args) {
6+
Hashtable<Integer, String> ht = new Hashtable<>();
7+
ht.put(1, "A");
8+
ht.put(2, "BZ"); // assume compute already applied
9+
ht.put(3, "C");
10+
ht.put(7, "Z7");
11+
12+
// Enumeration of values
13+
Enumeration<String> valuesEnum = ht.elements();
14+
15+
System.out.println("Values via Enumeration:");
16+
while (valuesEnum.hasMoreElements()) {
17+
String val = valuesEnum.nextElement();
18+
System.out.println(val);
19+
}
20+
21+
// Enumeration of keys
22+
Enumeration<Integer> keysEnum = ht.keys();
23+
24+
System.out.println("\nKeys via Enumeration:");
25+
while (keysEnum.hasMoreElements()) {
26+
Integer k = keysEnum.nextElement();
27+
System.out.println(k);
28+
}
29+
30+
/*
31+
- Enumeration is the old style for iterating Hashtable.
32+
- For modern collections prefer Iterator or forEach.
33+
- Hashtable is synchronized; Enumeration does not support fail-fast semantics like Iterator.
34+
iterating values() and keys(), Shows legacy-style iteration used with Hashtable
35+
(Enumeration rather than Iterator).
36+
*/
37+
}
38+
}

0 commit comments

Comments
 (0)