Exemples Java - Parcours de collection

Java 实例Exemples Java

Les exemples suivants montrent comment parcourir les collections qui étendent l'interface Collection, à savoir les collections de type List, Set et Map qui stockent des données sous forme de paires clé-valeur. Ci-dessous, nous utilisons la boucle for ordinaire, la boucle for améliorée, l'itérateur et d'autres méthodes pour parcourir les collections :

Parcours des collections de type List et Set

Fichier 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) { //Parcours des collections List listTest(); //Parcours des collections Set setTest(); } private static void setTest() { Set<String> set = new HashSet<String>(); set.add("JAVA"); set.add("C"); set.add("C++"); //Échec de l'ajout de données en double set.add("JAVA"); set.add("JAVASCRIPT"); //Utilisation d'un itérateur pour parcourir les collections Set Iterator<String> it = set.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println(value); } //Utilisation d'une boucle for améliorée pour parcourir les collections Set for(String s: set){ System.out.println(s); } } //Parcours des collections de listes 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"); //Utilisation d'un itérateur pour parcourir Iterator<String> it = list.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println(value); } //Utilisation d'une boucle for traditionnelle pour parcourir for (int i = 0, size = list.size(); i < size; i++) { String value = list.get(i); System.out.println(value); } //Utilisation d'une boucle for améliorée pour parcourir for (String value : list) { System.out.println(value); } } }

La sortie du code ci-dessus est :

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

À propos du parcours des collections de type Map

Dans l'exemple suivant, nous utilisons les méthodes keySet() et entrySet() de HashMap pour parcourir la 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);
      }
   }
}

La sortie du code ci-dessus est :

1 : PHP
2 : Java
3 : C
4 : C++
5 : HTML

Java 实例Exemples Java

Autres extensions