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

Java Example – HashMap Traversal

Java Example – HashMap Traversal

Java Examples Java Examples

The following example demonstrates how to use the iterator() method of the Collection class to traverse the collection:

Main.java File

import

java

.

util

.*;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

HashMap

<

String

,

String

hMap

=

new

HashMap

<

String

,

String

(

)

;

hMap

.

put

(

"

1

"

,

"

1st

"

)

;

hMap

.

put

(

"

2

"

,

"

2nd

"

)

;

hMap

.

put

(

"

3

"

,

"

3rd

"

)

;

Collection

cl

=

hMap

.

values

(

)

;

Iterator

itr

=

cl

.

iterator

(

)

;

while

(

itr

.

hasNext

(

)

)

{

System

.

out

.

println

(

itr

.

next

(

)

)

;

}

}

}

The output of the above code is:



1st

2nd

3rd

Java Examples Java Examples