-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerationDemo.java
More file actions
128 lines (112 loc) · 5.12 KB
/
Copy pathEnumerationDemo.java
File metadata and controls
128 lines (112 loc) · 5.12 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package Phase5_CollectionsLambdasStreams.Collections;
import java.util.*;
/**
* java.util.Enumeration<E> - The Original (Java 1.0) Iterator
* -----------------------------------------------------------
* Enumeration is the LEGACY ancestor of Iterator. It is the iteration
* contract used by the original Java 1.0 collection classes:
* <p>
*
* Vector / Stack / Hashtable
* StringTokenizer
* ZipFile / NetworkInterface (some IO classes still expose Enumerations)
* <p>
*
* Methods
* -------
* boolean hasMoreElements()
* E nextElement()
* default Iterator<E> asIterator() (Java 9+, useful bridge)
* <p>
*
* Enumeration vs Iterator
* -----------------------
* Enumeration Iterator (modern)
* -------------------- --------------------
* Java 1.0 Java 1.2
* hasMoreElements() hasNext()
* nextElement() next()
* - no remove remove() (optional)
* - no forEach (until 9) forEachRemaining()
* <p>
*
* Iterator superseded Enumeration in Java 1.2. The Collections Framework
* uses Iterator everywhere. Enumeration is kept for backwards compatibility
* with very old APIs.
* <p>
*
* When You Still See It
* ---------------------
* - servlet APIs: `HttpServletRequest.getHeaderNames()` returns an
* Enumeration<String> (kept for compatibility).
* - some classloader APIs: `getResources(name)` returns Enumeration<URL>.
* - StringTokenizer (also legacy - prefer String.split or Scanner).
* - any code targeting JDK 1.1 or older libraries.
* <p>
*
* Bridging Enumeration <-> Iterator
* ---------------------------------
* Enumeration.asIterator() Java 9+
* Collections.enumeration(Collection) legacy direction
* Collections.list(Enumeration) drain to a List
*/
public class EnumerationDemo {
public static void main(String[] args) {
section("1) Vector returns BOTH Enumeration and Iterator");
Vector<String> v = new Vector<>(List.of("a", "b", "c"));
Enumeration<String> en = v.elements(); // 1.0 style
Iterator<String> it = v.iterator(); // modern
System.out.print("Enumeration: "); while (en.hasMoreElements()) System.out.print(en.nextElement() + " ");
System.out.println();
System.out.print("Iterator : "); while (it.hasNext()) System.out.print(it.next() + " ");
System.out.println();
section("2) Hashtable - Enumeration over keys / values");
Hashtable<String, Integer> ht = new Hashtable<>();
ht.put("alpha", 1); ht.put("beta", 2); ht.put("gamma", 3);
System.out.print("keys() : "); for (Enumeration<String> e = ht.keys(); e.hasMoreElements(); ) System.out.print(e.nextElement() + " ");
System.out.println();
System.out.print("elements(): "); for (Enumeration<Integer> e = ht.elements(); e.hasMoreElements(); ) System.out.print(e.nextElement() + " ");
System.out.println();
section("3) StringTokenizer - another Enumeration source");
StringTokenizer st = new StringTokenizer("Hello world from Java");
while (st.hasMoreElements()) {
System.out.println(" token: " + st.nextToken());
}
// Modern equivalent of the loop above:
// for (String tok : "Hello world from Java".split("\\s+")) ...
section("4) Bridge - Enumeration.asIterator (Java 9+)");
Enumeration<String> en2 = v.elements();
Iterator<String> it2 = en2.asIterator();
it2.forEachRemaining(s -> System.out.print(s + " "));
System.out.println();
section("5) Bridge - Collections.enumeration(Collection) and Collections.list");
Enumeration<Integer> fromList = Collections.enumeration(List.of(1, 2, 3));
System.out.print("converted to enum: ");
while (fromList.hasMoreElements()) System.out.print(fromList.nextElement() + " ");
System.out.println();
Enumeration<String> srcEnum = v.elements();
List<String> drainedList = Collections.list(srcEnum);
System.out.println("drained to list = " + drainedList);
section("6) Real-world Enumeration - ClassLoader resources");
try {
Enumeration<java.net.URL> urls =
Thread.currentThread().getContextClassLoader().getResources("");
int count = 0;
while (urls.hasMoreElements() && count++ < 5) {
System.out.println(" resource: " + urls.nextElement());
}
if (count == 0) System.out.println(" (no resources in this run)");
} catch (java.io.IOException e) {
System.out.println(" (IO failure: " + e.getMessage() + ")");
}
section("7) Don't use Enumeration in new code");
// Use the for-each loop directly. The compiler turns this into
// Iterator-based iteration:
for (String s : v) System.out.print(s + " ");
System.out.println();
// OUTPUT (representative)
}
private static void section(String title) {
System.out.println("\n====== " + title + " ======");
}
}