Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Traversing the Keys and Values of a HashTable

Java Example – Traversing the Keys and Values of a HashTable

Java Examples Java Examples

The following example demonstrates how to use the keys() method of the Hashtable class to traverse and output the keys and values:

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

.

keys

(

)

;

while

(

e

.

hasMoreElements

(

)

)

{

System

.

out

.

println

(

e

.

nextElement

(

)

)

;

}

}

}

The output of the above code is:



3

2

1

Java Examples Java Examples