Java Examples - Collection Traversal

Java 实例Java Examples

The following examples demonstrate how to traverse collections that extend from the Collection interface, namely List, Set, and Map type collections that store data in key-value pairs. Below we use ordinary for, enhanced for, iterator, and other methods 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++"); //Duplicate data addition failed set.add("JAVA"); set.add("JAVASCRIPT"); //Using iterator to traverse set collections Iterator<String> it = set.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println(value); } //Using enhanced for loop to traverse set collections for(String s: set){ System.out.println(s); } } //Traversing list collections private static void listTest() { List<String> list = new ArrayList<String>(); list.add("Vegetable"); list.add("Bird"); list.add("Teach"); list.add("Course"); list.add("www.example.com"); //Using iterator to traverse Iterator<String> it = list.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println(value); } //Using traditional for loop to traverse for (int i = 0, size = list.size(); i < size; i++) { String value = list.get(i); System.out.println(value); } //Using enhanced for loop to traverse for (String value : list) { System.out.println(value); } } }

The output of the above code is:

菜
鸟
教
程
www.example.com
菜
鸟
教
程
www.example.com
菜
鸟
教
程
www.example.com
JAVA
JAVASCRIPT
C++
C
JAVA
JAVASCRIPT
C++
C

About Traversal of Map Type Collections

In the following example, we use HashMap's keySet() and entrySet() methods to traverse the collection:

/*
 author by example.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;

//增强For循环
public class Main {

   public static void main(String[] args) {
      // 创建一个HashMap对象,并加入了一些键值对。
      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");
      
      // 传统的遍历map集合的方法1; keySet()
      //traditionalMethod1(maps);
      // 传统的遍历map集合的方法2; entrySet()
      //traditionalMethod2(maps);
      // 使用增强For循环来遍历map集合方法1; keySet()
      //strongForMethod1(maps);
      // 使用增强For循环来遍历map集合方法2; 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);
      }
   }

   // 使用entrySet()方法,获取maps集合中的每一个键值对,
   private static void traditionalMethod2(Map<String, String> maps) {
      Set<Map.Entry<String, String>> sets = maps.entrySet();
      // 取得迭代器遍历出对应的值。
      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);
      }
   }

   // 使用keySet()方法,获取maps集合中的所有键,遍历键取得所对应的值。
   private static void traditionalMethod1(Map<String, String> maps) {
      Set<String> sets = maps.keySet();
      // 取得迭代器遍历出对应的值
      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

Java 实例Java Examples

Other Extensions