Java Example – Traversing a HashTable Using Enumeration
The following example demonstrates how to use the hasMoreElements and nextElement methods of the Enumeration class to traverse and output the contents of a HashTable:
Main.java File
import
java
.
util
.
Enumeration
;
import
java
.
util
.
Hashtable
;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
Hashtable
ht
=
new
Hashtable
(
)
;
ht
.
put
(
"
1
"
,
"
One
"
)
;
ht
.
put
(
"
2
"
,
"
Two
"
)
;
ht
.
put
(
"
3
"
,
"
Three
"
)
;
Enumeration
e
=
ht
.
elements
(
)
;
while
(
e
.
hasMoreElements
(
)
)
{
System
.
out
.
println
(
e
.
nextElement
(
)
)
;
}
}
}
The output of the above code is:
Three
Two
One
Java Examples