java集合類的使用可以說是無處不在,總的我們可以將之分為三大塊,分別是從Collection接口延伸出的List、Set和以鍵值對形式作存儲的Map類型集合。
許多情況需要我們遍歷出集合中的元素,並做相應的處理。
下面對各種類型的集合的遍歷做一些總結,關於增強for循環,需要注意的是,使用增強for循環無法訪問數組下標值,對於集合的遍歷其內部采用的也是Iterator的相關方法。如果只做簡單遍歷讀取,增強for循環確實減輕不少的代碼量。
關於List與Set類型集合的遍歷:
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 ListAndSetTest {
8
9 public static void main(String[] args) {
10 // List集合的遍歷
11 listTest();
12 // Set集合的遍歷
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 //set集合遍歷方法1,使用iterator
26 Iterator<String> it = set.iterator();
27 while (it.hasNext()) {
28 String value = it.next();
29 System.out.println(value);
30 }
31
32 //set集合遍歷方法2,使用增強for循環。
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("java111");
42 list.add("java222");
43 list.add("java333");
44 list.add("java444");
45 list.add("java555");
46
47 // 遍歷方式1 ,使用iterator
48 Iterator<String> it = list.iterator();
49 while (it.hasNext()) {
50 String value = it.next();
51 System.out.println(value);
52 }
53
54 // 遍歷方法2 , 使用傳統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 // 遍歷方法3 , 使用增強for循環進行遍歷。
61 for (String value : list) {
62 System.out.println(value);
63 }
64 }
65 }
關於Map類型集合的遍歷,keySet()與entrySet()方法:
1 //增強For循環
2 public class MapTest {
3
4 public static void main(String[] args) {
5 // 創建一個HashMap對象,並加入了一些鍵值對。
6 Map<String, String> maps = new HashMap<String, String>();
7 maps.put("111", "java111");
8 maps.put("222", "java222");
9 maps.put("333", "java333");
10 maps.put("444", "java444");
11 maps.put("555", "java555");
12
13 // 傳統的遍歷map集合的方法1; keySet()
14 //traditionalMethod1(maps);
15 // 傳統的遍歷map集合的方法2; entrySet()
16 //traditionalMethod2(maps);
17 // 使用增強For循環來遍歷map集合方法1; keySet()
18 //strongForMethod1(maps);
19 // 使用增強For循環來遍歷map集合方法2; entrySet()
20 strongForMethod2(maps);
21 }
22
23 private static void strongForMethod2(Map<String, String> maps) {
24 Set<Entry<String, String>> set = maps.entrySet();
25 for (Entry<String, String> entry : set) {
26 String key = entry.getKey();
27 String value = entry.getValue();
28 System.out.println(key + " : " + value);
29 }
30 }
31
32 private static void strongForMethod1(Map<String, String> maps) {
33 Set<String> set = maps.keySet();
34 for (String s : set) {
35 String key = s;
36 String value = maps.get(s);
37 System.out.println(key + " : " + value);
38 }
39 }
40
41 // 使用entrySet()方法,獲取maps集合中的每一個鍵值對,
42 private static void traditionalMethod2(Map<String, String> maps) {
43 Set<Map.Entry<String, String>> sets = maps.entrySet();
44 // 取得迭代器遍歷出對應的值。
45 Iterator<Entry<String, String>> it = sets.iterator();
46 while (it.hasNext()) {
47 Map.Entry<String, String> entry = (Entry<String, String>) it.next();
48 String key = entry.getKey();
49 String value = entry.getValue();
50 System.out.println(key + " : " + value);
51 }
52 }
53
54 // 使用keySet()方法,獲取maps集合中的所有鍵,遍歷鍵取得所對應的值。
55 private static void traditionalMethod1(Map<String, String> maps) {
56 Set<String> sets = maps.keySet();
57 // 取得迭代器遍歷出對應的值。
58 Iterator<String> it = sets.iterator();
59 while (it.hasNext()) {
60 String key = it.next();
61 String value = maps.get(key);
62 System.out.println(key + " : " + value);
63 }
64 }
65 }

