Date: 2020dec8
Language: Java
Keywords: Enumeration, String
Q. Java: iterate thru an Enumeration<String>
A.
An example of where you get an Enumeration<String>
HttpSession sess = request.getSession();
Enumeration<String> names = sess.getNames();
If there is a small number of items, I prefer to convert it to a list
for (final String name : Collections.list(names)) {
System.out.println(name);
}
But if there is a large number, that becomes wasteful so go with
while (names.hasMoreElements()) {
final String name = name.nextElement();
System.out.println(name);
}