1. Map的四種遍歷方式
下面只是簡單介紹各種遍歷示例(以HashMap為例),各自優劣會在本文后面進行分析給出結論。
(1) for each map.entrySet()
1
2
3
4
5
|
Map<String, String> map = new HashMap<String, String>();
for (Entry<String, String> entry : map.entrySet()) {
entry.getKey();
entry.getValue();
}
|
(2) 顯示調用map.entrySet()的集合迭代器
1
2
3
4
5
6
|
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
entry.getKey();
entry.getValue();
}
|
(3) for each map.keySet(),再調用get獲取
1
2
3
4
|
Map<String, String> map = new HashMap<String, String>();
for (String key : map.keySet()) {
map.get(key);
}
|
(4) for each map.entrySet(),用臨時變量保存map.entrySet()
1
2
3
4
5
|
Set<Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
entry.getKey();
entry.getValue();
}
|
在測試前大家可以根據對HashMap的了解,想想上面四種遍歷方式哪個性能更優。
2、HashMap四種遍歷方式的性能測試及對比
以下是性能測試代碼,會輸出不同數量級大小的HashMap各種遍歷方式所花費的時間。
PS:如果運行報異常in thread “main” java.lang.OutOfMemoryError: Java heap space,請將main函數里面map size的大小減小。
其中getHashMaps函數會返回不同size的HashMap。
loopMapCompare函數會分別用上面的遍歷方式1-4去遍歷每一個map數組(包含不同大小HashMap)中的HashMap。
print開頭函數為輸出輔助函數,可忽略。
測試環境為Windows7 32位系統 3.2G雙核CPU 4G內存,Java 7,Eclipse -Xms512m -Xmx512m
最終測試結果如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
compare loop performance of HashMap
-----------------------------------------------------------------------
map size | 10,000 | 100,000 | 1,000,000 | 2,000,000
-----------------------------------------------------------------------
for each entrySet | 2 ms | 6 ms | 36 ms | 91 ms
-----------------------------------------------------------------------
for iterator entrySet | 0 ms | 4 ms | 35 ms | 89 ms
-----------------------------------------------------------------------
for each keySet | 1 ms | 6 ms | 48 ms | 126 ms
-----------------------------------------------------------------------
for entrySet=entrySet()| 1 ms | 4 ms | 35 ms | 92 ms
-----------------------------------------------------------------------
|
表橫向為同一遍歷方式不同大小HashMap遍歷的時間消耗,縱向為同一HashMap不同遍歷方式遍歷的時間消耗。
PS:由於首次遍歷HashMap會稍微多耗時一點,for each的結果稍微有點偏差,將測試代碼中的幾個Type順序調換會發現,for each entrySet耗時和for iterator entrySet接近。
3、遍歷方式性能測試結果分析
(1) foreach介紹
見:ArrayList和LinkedList的幾種循環遍歷方式及性能對比分析中介紹。
(2) HashMap遍歷方式結果分析
從上面知道for each與顯示調用Iterator等價,上表的結果中可以看出除了第三種方式(for each map.keySet()),再調用get獲取方式外,其他三種方式性能相當。本例還是hash值散列較好的情況,若散列算法較差,第三種方式會更加耗時。
我們看看HashMap entrySet和keySet的源碼
1
2
3
4
5
6
7
8
9
10
11
|
private final class KeyIterator extends HashIterator<K> {
public K next() {
return nextEntry().getKey();
}
}
private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
public Map.Entry<K,V> next() {
return nextEntry();
}
}
|
分別是keySet()和entrySet()返回的set的迭代器,從中我們可以看到只是返回值不同而已,父類相同,所以性能相差不多。只是第三種方式多了一步根據key get得到value的操作而已。get的時間復雜度根據hash算法而異,源碼如下:
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
|
public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}
/**
* Returns the entry associated with the specified key in the
* HashMap. Returns null if the HashMap contains no mapping
* for the key.
*/
final Entry<K,V> getEntry(Object key) {
int hash = (key == null) ? 0 : hash(key);
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
|
get的時間復雜度取決於for循環循環次數,即hash算法。
4、結論總結
從上面的分析來看:
a. HashMap的循環,如果既需要key也需要value,直接用
1
2
3
4
5
|
Map<String, String> map = new HashMap<String, String>();
for (Entry<String, String> entry : map.entrySet()) {
entry.getKey();
entry.getValue();
}
|
即可,foreach簡潔易懂。
b. 如果只是遍歷key而無需value的話,可以直接用
1
2
3
4
|
Map<String, String> map = new HashMap<String, String>();
for (String key : map.keySet()) {
// key process
}
|