Traversal of List and Set type collections
1 import java.util.ArrayList; 2 import java.util.HashSet; 3 import java.util.Iterator; 4 import java.util.List; 5 import java.util.Set; 6 7 public class Main { 8 9 public static void main(String[] args) { 10 // Traversal of List collection 11 listTest(); 12 // Traversal of Set collection 13 setTest(); 14 } 15 16 private static void setTest() { 17 Set<String> set = new HashSet<String>(); 18 set.add("JAVA"); 19 set.add("C"); 20 set.add("C++"); 21 // 重复数据添加失败 22 set.add("JAVA"); 23 set.add("JAVASCRIPT"); 24 25 // 使用iterator遍历set集合 26 Iterator<String> it = set.iterator(); 27 while (it.hasNext()) { 28 String value = it.next(); 29 System.out.println(value); 30 } 31 32 // 使用增强for循环遍历set集合 33 for(String s: set){ 34 System.out.println(s); 35 } 36 } 37 38 // 遍历list集合 39 private static void listTest() { 40 List<String> list = new ArrayList<String>(); 41 list.add("W"); 42 list.add("M"); 43 list.add("L"); 44 list.add("D"); 45 list.add("www.pekst.com"); 46 47 // 使用iterator遍历 48 Iterator<String> it = list.iterator(); 49 while (it.hasNext()) { 50 String value = it.next(); 51 System.out.println(value); 52 } 53 54 // 使用传统for循环进行遍历 55 for (int i = 0, size = list.size(); i < size; i++) { 56 String value = list.get(i); 57 System.out.println(value); 58 } 59 60 // Use enhanced for loop to traverse 61 for (String value : list) { 62 System.out.println(value); 63 } 64 } 65 }
Traversal of Map type collections
In the following example we use the keySet() and entrySet() methods of HashMap to traverse the collection:
1 import java.util.Map; 2 import java.util.HashMap; 3 import java.util.HashSet; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Set; 7 import java.util.Map.Entry; 8 9 // Enhanced For loop 10 public class Main { 11 12 public static void main(String[] args) { 13 // Create a HashMap object and add some key-value pairs. 14 Map<String, String> maps =new HashMap<String, String> (); 15 maps.put("1", "PHP" ); 16 maps.put("2", "Java" ); 17 maps.put("3", "C" ); 18 maps.put("4", "C++" ); 19 maps.put("5", "HTML" ); 20 21 // traditional method 1 of traversing the map set; keySet() 22 // traditionalMethod1 (maps); 23 // Traditional method 2 of traversing map collection; entrySet() 24 // traditionalMethod2(maps); 25 // Using enhanced For loop to traverse map collection method 1; keySet() 26 //strongForMethod1(maps); 27 // 使用增强For循环来遍历map集合方法2; entrySet() 28 strongForMethod2(maps); 29 } 30 31 private static void strongForMethod2(Map<String, String> maps) { 32 Set<Entry<String, String>> set = maps.entrySet(); 33 for (Entry<String, String> entry : set) { 34 String key = entry.getKey(); 35 String value = entry.getValue(); 36 System.out.println(key + " : " + value); 37 } 38 } 39 40 private static void strongForMethod1(Map<String, String> maps) { 41 Set<String> set = maps.keySet(); 42 for (String s : set) { 43 String key = s; 44 String value = maps.get (s); 45 System.out.println(key + " : " + value); 46 } 47 } 48 49 // Use the entrySet() method to get each key-value pair in the maps collection, 50 private static void traditionalMethod2 (Map<String, String> maps) { 51 Set<Map.Entry<String, String>> sets = maps.entrySet(); 52 // Get the iterator to traverse the corresponding value. 53 Iterator<Entry<String, String>> it = sets.iterator(); 54 while (it.hasNext()) { 55 Map.Entry<String, String> entry = (Entry<String, String> ) it.next (); 56 String key = entry.getKey(); 57 String value = entry.getValue(); 58 System.out.println(key + " : " + value); 59 } 60 } 61 62 // Use keySet( ) method to get all the keys in the maps collection, traverse the keys to get the corresponding values. 63 private static void traditionalMethod1(Map<String, String> maps) { 64 Set<String> sets = maps.keySet(); 65 // 取得迭代器遍历出对应的值 66 Iterator<String> it = sets.iterator(); 67 while (it.hasNext()) { 68 String key = it.next(); 69 String value = maps.get(key); 70 System.out.println(key + " : " + value); 71 } 72 } 73 }