詳解Java中list,set,map的遍歷與增強for循環(huán)
Java集合類可分為三大塊,分別是從Collection接口延伸出的List、Set和以鍵值對形式作存儲的Map類型集合。
關于增強for循環(huán),需要注意的是,使用增強for循環(huán)無法訪問數(shù)組下標值,對于集合的遍歷其內(nèi)部采用的也是Iterator的相關方法。如果只做簡單遍歷讀取,增強for循環(huán)確實減輕不少的代碼量。
集合概念:
1.作用:用于存放對象
2.相當于一個容器,里面包含著一組對象,其中的每個對象作為集合的一個元素出現(xiàn)
3.java的容器有集合類和數(shù)組,不同之處是
區(qū)別及其常用實現(xiàn)類
List接口:
列表有序 元素可重復
實現(xiàn)類:ArrayList:動態(tài)數(shù)組列表
LinkedList:雙向鏈表
Set接口:
集無序,元素不可重復
實現(xiàn)類:HashSet:散列集
TreeSet:樹集 內(nèi)部排序
Map接口:
以鍵值對的方式存儲數(shù)據(jù) 數(shù)據(jù)-鍵不允許重復
實現(xiàn)類:HashSet:散列集
TreeSet:樹集 內(nèi)部排序
JDK1.0出現(xiàn)的集合類都是線程安全的,但效率低
JDK1.2出現(xiàn)的集合類都不是線程安全的,但效率高
代碼示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; public class ListAndSet{ public static void main(String[] args) { setTest(); listTest(); } // 遍歷Set集合 private static void setTest() { Set<string> set = new HashSet<string>(); set.add( "A" ); set.add( "B" ); set.add( "C" ); set.add( "D" ); set.add( "E" ); //set集合遍歷方法1:使用iterator Iterator<string> it = set.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println(value); } //set集合遍歷方法2:使用增強for循環(huán)。 for (String s: set){ System.out.println(s); } } // 遍歷list集合 private static void listTest() { List<string> list = new ArrayList<string>(); list.add( "111" ); list.add( "222" ); list.add( "333" ); list.add( "444" ); list.add( "555" ); // 遍歷方式1:使用iterator Iterator<string> it = list.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println(value); } // 遍歷方法2:使用傳統(tǒng)for循環(huán)進行遍歷。 for ( int i = 0 , size = list.size(); i < size; i++) { String value = list.get(i); System.out.println(value); } // 遍歷方法3:使用增強for循環(huán)進行遍歷。 for (String value : list) { System.out.println(value); } } } //關于Map類型集合的遍歷,keySet()與entrySet()方法 //增強For循環(huán) public class Map{ public static void main(String[] args) { // 創(chuàng)建一個HashMap對象,并加入了一些鍵值對。 Map<string, string= "" > maps = new HashMap<string, string= "" >(); maps.put( "111" , "111" ); maps.put( "222" , "222" ); maps.put( "333" , "333" ); maps.put( "444" , "444" ); maps.put( "555" , "555" ); // 傳統(tǒng)的遍歷map集合的方法1; keySet() //traditionalMethod1(maps); // 傳統(tǒng)的遍歷map集合的方法2; entrySet() //traditionalMethod2(maps); // 使用增強For循環(huán)來遍歷map集合方法1; keySet() //strongForMethod1(maps); // 使用增強For循環(huán)來遍歷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); } } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://www.2cto.com/kf/201702/596383.html