Home > Java > javaTutorial > Java Example - Collection Traversal

Java Example - Collection Traversal

黄舟
Release: 2017-01-21 11:07:47
Original
1272 people have browsed it

The following example demonstrates how to traverse a collection of List, Set and Map types that are stored in the form of key-value pairs extended from the Collection interface. Below we use ordinary for, enhanced for, iterator and other methods to traverse. Collections:

Traversal of List and Set type collections

/*
 author by w3cschool.cc
 Main.java
 */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) {
      // List集合的遍历
      listTest();
      // Set集合的遍历
      setTest();
   }

   private static void setTest() {
      Set<String> set = new HashSet<String>();
      set.add("JAVA");
      set.add("C");
      set.add("C++");
      // 重复数据添加失败
      set.add("JAVA");
      set.add("JAVASCRIPT");

      // 使用iterator遍历set集合
      Iterator<String> it = set.iterator();
      while (it.hasNext()) {
         String value = it.next();
         System.out.println(value);
      }
      
      // 使用增强for循环遍历set集合
      for(String s: set){
         System.out.println(s);
      }
   }

   // 遍历list集合
   private static void listTest() {
      List<String> list = new ArrayList<String>();
      list.add("菜");
      list.add("鸟");
      list.add("教");
      list.add("程");
      list.add("www.w3cschool.cc");

      // 使用iterator遍历
      Iterator<String> it = list.iterator();
      while (it.hasNext()) {
         String value = it.next();
         System.out.println(value);
      }

      // 使用传统for循环进行遍历
      for (int i = 0, size = list.size(); i < size; i++) {
         String value = list.get(i);
         System.out.println(value);
      }

      // 使用增强for循环进行遍历
      for (String value : list) {
         System.out.println(value);
      }
   }}
Copy after login

The output result of running the above code is:

菜鸟教程www.w3cschool.cc菜鸟教程www.w3cschool.cc菜鸟教程www.w3cschool.cc
JAVA
JAVASCRIPT
C++C
JAVA
JAVASCRIPT
C++C
Copy after login

About traversal of Map type collections

The following examples We used the keySet() and entrySet() methods of HashMap to traverse the collection:

/*
 author by w3cschool.cc
 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);
      }
   }}
Copy after login


The output result of running the above code is:

1 : PHP
2 : Java
3 : C
4 : C++
5 : HTML
Copy after login

The above is Java Example - Collection traversal content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template