标签:style blog color 使用 sp for strong 数据 div
keySet是键的集合,Set里面的类型即key的类型
entrySet是 键-值 对的集合,Set里面的类型是Map.Entry
keySet()的速度比entrySet()慢了很多
使用entrySet则必须将map对象转换为Map.Entry,keySet则不需要
遍历map
//方法一 Set<String> set = map.keySet(); for (String s:set) { System.out.println(s+","+map.get(s)); } //方法二 Set<Map.Entry<String, String>> entryseSet=map.entrySet(); for (Map.Entry<String, String> entry:entryseSet) { System.out.println(entry.getKey()+","+entry.getValue()); }
一开始只进行了简单的测试,但结果却表明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); }
or (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(); }
or (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()) { } |
标签:style blog color 使用 sp for strong 数据 div
原文地址:http://www.cnblogs.com/mimimimimi/p/4094265.html