对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多。理由是:entrySet方法一次拿到所有key和value的集合;而keySet拿到的只是key的集合,针对每个key,都要去Map中额外查找一次value,从而降低了总体效率。那么实际情况如何呢?
为了解遍历性能的真实差距,包括在遍历key+value、遍历key、遍历value等不同场景下的差异,我试着进行了一些对比测试。
一开始只进行了简单的测试,但结果却表明keySet的性能更好,这一点让我很是费解,不都说entrySet明显好于keySet吗?为了进一步地进行验证,于是采用了不同的测试数据进行更详细的对比测试。
Map<String, String> map = new HashMap<String, String>(); String key, value; for (i = 1; i <= num; i++) { key = "" + i; value = "value"; map.put(key, value); } |
Map<String, String> map = new HashMap<String, String>(); String key, value; for (i = 1; i <= num; i++) { key = "" + (i * 50); value = "value"; map.put(key, value); } |
2.1.2 TreeMap测试数据
Map<String, String> map = new TreeMap<String, String>(); String key, value; for (i = 1; i <= num; i++) { key = "" + i; value = "value"; map.put(key, value); } |
Map<String, String> map = new TreeMap<String, String>(); String key, value; for (i = 1; i <= num; i++) { key = "" + (i * 50); value = "value"; map.put(key, value); } |
2.2 测试场景
分别使用keySet、entrySet和values的多种写法测试三种场景:遍历key+value、遍历key、遍历value的场景。
Iterator<String> iter = map.keySet().iterator(); while (iter.hasNext()) { key = iter.next(); value = map.get(key); } |
for (String key : map.keySet()) { value = map.get(key); } |
Iterator<Entry<String, String>> iter = map.entrySet().iterator(); Entry<String, String> entry; while (iter.hasNext()) { entry = iter.next(); key = entry.getKey(); value = entry.getValue(); } |
for (Entry<String, String> entry: map.entrySet()) { key = entry.getKey(); value = entry.getValue(); } |
Iterator<String> iter = map.keySet().iterator(); while (iter.hasNext()) { key = iter.next(); } |
for (String key : map.keySet()) { } |
Iterator<Entry<String, String>> iter = map.entrySet().iterator(); while (iter.hasNext()) { key = iter.next().getKey(); } |
for (Entry<String, String> entry: map.entrySet()) { key = entry.getKey(); } |
2.2.3 遍历value
Iterator<String> iter = map.keySet().iterator(); while (iter.hasNext()) { value = map.get(iter.next()); } |
for (String key : map.keySet()) { value = map.get(key); } |
Iterator<Entry<String, String>> iter = map.entrySet().iterator(); while (iter.hasNext()) { value = iter.next().getValue(); } |
for (Entry<String, String> entry: map.entrySet()) { value = entry.getValue(); } |
Iterator<String> iter = map.values().iterator(); while (iter.hasNext()) { value = iter.next(); } |
for (String value : map.values()) { } |
2.3 测试结果
单位:毫秒 |
HashMap-1 |
HashMap-2 |
keySet遍历key+value(写法1) |
39 |
93 |
keySet遍历key+value(写法2) |
38 |
87 |
entrySet遍历key+value(写法1) |
43 |
86 |
entrySet遍历key+value(写法2) |
43 |
85 |
单位:毫秒 |
HashMap-1 |
HashMap-2 |
keySet遍历key(写法1) |
27 |
65 |
keySet遍历key(写法2) |
26 |
64 |
entrySet遍历key(写法1) |
35 |
75 |
entrySet遍历key(写法2) |
34 |
74 |
单位:毫秒 |
HashMap-1 |
HashMap-2 |
keySet遍历value(写法1) |
38 |
87 |
keySet遍历value(写法2) |
37 |
87 |
entrySet遍历value(写法1) |
34 |
61 |
entrySet遍历value(写法2) |
32 |
62 |
values遍历value(写法1) |
26 |
48 |
values遍历value(写法2) |
26 |
48 |
2.3.2 TreeMap测试结果
单位:毫秒 |
TreeMap-1 |
TreeMap-2 |
keySet遍历key+value(写法1) |
430 |
451 |
keySet遍历key+value(写法2) |
429 |
450 |
entrySet遍历key+value(写法1) |
77 |
84 |
entrySet遍历key+value(写法2) |
70 |
68 |
单位:毫秒 |
TreeMap-1 |
TreeMap-2 |
keySet遍历key(写法1) |
50 |
49 |
keySet遍历key(写法2) |
49 |
48 |
entrySet遍历key(写法1) |
66 |
64 |
entrySet遍历key(写法2) |
65 |
63 |
单位:毫秒 |
TreeMap-1 |
TreeMap-2 |
keySet遍历value(写法1) |
432 |
448 |
keySet遍历value(写法2) |
430 |
448 |
entrySet遍历value(写法1) |
62 |
61 |
entrySet遍历value(写法2) |
62 |
61 |
values遍历value(写法1) |
46 |
46 |
values遍历value(写法2) |
45 |
46 |
3. 结论
for (String key : map.keySet()) { value = map.get(key); } |
for (Entry<String, String> entry: map.entrySet()) { key = entry.getKey(); value = entry.getValue(); } |
for (String value : map.values()) { } |
3.2 如果你使用TreeMap
for (String key : map.keySet()) { value = map.get(key); } |
for (Entry<String, String> entry: map.entrySet()) { key = entry.getKey(); value = entry.getValue(); } |
for (String value : map.values()) { } |