Java Example - Collection Traversal
The following example demonstrates how to traverse collections derived from the Collection interface, such as List, Set, and Map (which stores data in key-value pairs). We use regular for loops, enhanced for loops, and iterators to traverse these collections:
Traversal of List and Set 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 collection
listTest();
// Traversal of Set collection
setTest();
}
private static void setTest() {
Set<String> set = new HashSet<String>();
set.add("JAVA");
set.add("C");
set.add("C++");
// Duplicate data addition fails
set.add("JAVA");
set.add("JAVASCRIPT");
// Traverse set collection using iterator
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String value = it.next();
System.out.println(value);
}
// Traverse set collection using enhanced for loop
for(String s: set){
System.out.println(s);
}
}
// Traverse list collection
private static void listTest() {
List<String> list = new ArrayList<String>();
list.add("菜");
list.add("鸟");
list.add("教");
list.add("程");
list.add("www.tutorialpro.org");
// Traverse using iterator
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String value = it.next();
System.out.println(value);
}
// Traverse using traditional for loop
for (int i = 0, size = list.size(); i < size; i++) {
String value = list.get(i);
System.out.println(value);
}
// Traverse using enhanced for loop
for (String value : list) {
System.out.println(value);
}
}
}
The above code outputs the following results:
菜
鸟
教
程
www.tutorialpro.org
菜
鸟
教
程
www.tutorialpro.org
菜
鸟
教
程
www.tutorialpro.org
JAVA
JAVASCRIPT
C++
C
JAVA
JAVASCRIPT
C++
C
Traversal of Map Collections
The following example uses the keySet() and entrySet() methods of HashMap to traverse the collection:
/*
author by tutorialpro.org
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<String, String> maps = new HashMap<String, String>();
maps.put("1", "PHP");
maps.put("2", "Java");
maps.put("3", "C");
maps.put("4", "C++");
maps.put("5", "HTML");
// Traditional method to traverse the map collection using keySet()
//traditionalMethod1(maps);
// Traditional method to traverse the map collection using entrySet()
//traditionalMethod2(maps);
// Enhanced For loop to traverse the map collection using keySet()
//strongForMethod1(maps);
// Enhanced For loop to traverse the map collection using entrySet()
strongForMethod2(maps);
}
private static void strongForMethod2(Map<String, String> maps) {
Set<Entry<String, String>> set = maps.entrySet();
for (Entry<String, String> entry : set) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
private static void strongForMethod1(Map<String, String> maps) {
Set<String> set = maps.keySet();
for (String s : set) {
String key = s;
String value = maps.get(s);
System.out.println(key + " : " + value);
}
}
// Using entrySet() method to get each key-value pair in the maps collection
private static void traditionalMethod2(Map<String, String> maps) {
Set<Map.Entry<String, String>> sets = maps.entrySet();
// Get the iterator and traverse to get the corresponding values.
Iterator<Entry<String, String>> it = sets.iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = (Entry<String, String>) it.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
// Using keySet() method to get all keys in the maps collection and traverse to get the corresponding values.
private static void traditionalMethod1(Map<String, String> maps) {
Set<String> sets = maps.keySet();
// Get the iterator and traverse to get the corresponding values
Iterator<String> 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
1 : PHP 2 : Java 3 : C 4 : C++ 5 : HTML