Java Example – Collection Traversal
The following example demonstrates how to traverse List, Set (extending the Collection interface), and Map-type collections stored as key-value pairs. We use ordinary for loops, enhanced for loops, and iterators to traverse the collections:
Traversal of List and Set type collections
Main.java File
import
java
.
util
.
ArrayList
;
import
java
.
util
.
HashSet
;
import
java
.
util
.
Iterator
;
import
java
.
util
.
List
;
import
java
.
util
.
Set
;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
//
Traversal of List collections
listTest
(
)
;
//
Traversal of Set collections
setTest
(
)
;
}
private
static
void
setTest
(
)
{
Set
<
String
set
=
new
HashSet
<
String
(
);
set
.
add
(
"
JAVA
"
)
;
set
.
add
(
"
C
"
)
;
set
.
add
(
"
C++
"
)
;
//
Failed to add duplicate data
set
.
add
(
"
JAVA
"
)
;
set
.
add
(
"
JAVASCRIPT
"
)
;
//
Traverse the Set collection using an iterator
Iterator
<
String
it
=
set.
iterator
(
)
;
while
(
it
.
hasNext
(
)
)
{
String
value
=
it
.
next
(
)
;
System
.
out
.
println
(
value
)
;
}
//
Traverse the Set collection using an enhanced for loop
for
(
String
s
:
set
)
{
System
.
out
.
println
(
s
)
;
}
}
//
Traverse the List collection
private
static
void
listTest
(
)
{
List
<
String
list
=
new
ArrayList
<
String
(
);
list
.
add
(
"
Runoob
"
)
;
list
.
add
(
"
Tutorial
"
)
;
list
.
add
(
"
"
)
;
list
.
add
(
"
"
)
;
list
.
add
(
"
www.runoob.com
"
)
;
//
Traverse using an iterator
Iterator
<
String
it
=
list.
iterator
(
)
;
while
(
it
.
hasNext
(
)
)
{
String
value
=
it
.
next
(
)
;
System
.
out
.
println
(
value
)
;
}
//
Traverse using a traditional for loop
for
(
int
i
=
0
,
size
=
list
.
size
(
)
;
i
<
size
;
i
++
)
{
String
value
=
list
.
get
(
i
)
;
System
.
out
.
println
(
value
)
;
}
//
Traverse using an enhanced for loop
for
(
String
value
:
list
)
{
System
.
out
.
println
(
value
)
;
}
}
}
The output of the above code is:
菜
鸟
教
程
www.runoob.com
菜
鸟
教
程
www.runoob.com
菜
鸟
教
程
www.runoob.com
JAVA
JAVASCRIPT
C++
C
JAVA
JAVASCRIPT
C++
C
About the traversal of Map-type collections
In the following example, we use the keySet() and entrySet() methods of HashMap to traverse the collection:
/*
author by runoob.com
Main.java
*/
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;
//Enhanced For loop
public class Main {
public static void main(String[] args) {
// Create a HashMap object and add some key-value pairs.
Map maps = new HashMap();
maps.put("1", "PHP");
maps.put("2", "Java");
maps.put("3", "C");
maps.put("4", "C++");
maps.put("5", "HTML");
// Traditional method 1 for iterating map collection; keySet()
//traditionalMethod1(maps);
// Traditional method 2 for iterating map collections; entrySet()
//traditionalMethod2(maps);
// Use enhanced for loop to iterate map collection method 1; keySet()
//strongForMethod1(maps);
// Use enhanced for loop to iterate map collection method 2; entrySet()
strongForMethod2(maps);
}
private static void strongForMethod2(Map maps) {
Set> set = maps.entrySet();
for (Entry entry : set) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
private static void strongForMethod1(Map maps) {
Set set = maps.keySet();
for (String s : set) {
String key = s;
String value = maps.get(s);
System.out.println(key + " : " + value);
}
}
// Use the entrySet() method to get every key-value pair in the maps collection,
private static void traditionalMethod2(Map maps) {
Set> sets = maps.entrySet();
// Get the corresponding value from the iterator.
Iterator> it = sets.iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
// Use the keySet() method to get all keys in the map collection, and iterate over the keys to get the corresponding values.
private static void traditionalMethod1(Map maps) {
Set sets = maps.keySet();
// Get the corresponding value from the iterator
Iterator it = sets.iterator();
while (it.hasNext()) {
String key = it.next();
String value = maps.get(key);
System.out.println(key + " : " + value);
}
}
}
The output of the above code is:
1 : PHP
2 : Java
3 : C
4 : C++
5 : HTML
Java Examples