无意中看见代码扫描出的一些performance警告,大意是建议使用entrySet 代替KeySet对Map进行遍历。
测试了一下两者效率,发现确实使用entrySet更高效 大概高出30%-40%,原因是KeySet多了一次从Map中get的操作,虽然时间复杂度为常量级,但是对于大规模Map的遍历,还是有很大影响的。
1 public class Test {
2
3 public static void main(String[] args) {
4 Map<String,String> entity = createTestMap(1000000 , 5);
5 traversalBykeySet(entity);
6 traversalByEntry(entity);
7
8
9 }
10
11 private static void traversalByEntry(Map<String,String> entity){
12 long startTime = Calendar.getInstance().getTimeInMillis();
13 Iterator<Entry<String, String>> iter = entity.entrySet().iterator();
14 while (iter.hasNext()) {
15 Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();
16 entry.getKey();
17 entry.getValue();
18 }
19 System.out.println("CostTime:" + (Calendar.getInstance().getTimeInMillis() - startTime));
20 }
21
22 private static void traversalBykeySet(Map<String,String> entity){
23 long startTime = Calendar.getInstance().getTimeInMillis();
24 for(String key : entity.keySet()){
25 entity.get(key);
26 }
27 System.out.println("CostTime:" + (Calendar.getInstance().getTimeInMillis() - startTime));
28 }
29
30 private static Map<String,String> createTestMap(int size , int keyLength){
31 Map<String,String> entity = new HashMap<String,String>();
32 for(int i=0 ; i<size ; i++){
33 String randomString = getRandomString(keyLength);
34 entity.put(randomString, randomString);
35 }
36 return entity;
37 }
38
39 public static String getRandomString(int length) {
40 StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
41 StringBuffer sb = new StringBuffer();
42 Random r = new Random();
43 int range = buffer.length();
44 for (int i = 0; i < length; i ++) {
45 sb.append(buffer.charAt(r.nextInt(range)));
46 }
47 return sb.toString();
48 }
49
50 }
2
3 public static void main(String[] args) {
4 Map<String,String> entity = createTestMap(1000000 , 5);
5 traversalBykeySet(entity);
6 traversalByEntry(entity);
7
8
9 }
10
11 private static void traversalByEntry(Map<String,String> entity){
12 long startTime = Calendar.getInstance().getTimeInMillis();
13 Iterator<Entry<String, String>> iter = entity.entrySet().iterator();
14 while (iter.hasNext()) {
15 Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();
16 entry.getKey();
17 entry.getValue();
18 }
19 System.out.println("CostTime:" + (Calendar.getInstance().getTimeInMillis() - startTime));
20 }
21
22 private static void traversalBykeySet(Map<String,String> entity){
23 long startTime = Calendar.getInstance().getTimeInMillis();
24 for(String key : entity.keySet()){
25 entity.get(key);
26 }
27 System.out.println("CostTime:" + (Calendar.getInstance().getTimeInMillis() - startTime));
28 }
29
30 private static Map<String,String> createTestMap(int size , int keyLength){
31 Map<String,String> entity = new HashMap<String,String>();
32 for(int i=0 ; i<size ; i++){
33 String randomString = getRandomString(keyLength);
34 entity.put(randomString, randomString);
35 }
36 return entity;
37 }
38
39 public static String getRandomString(int length) {
40 StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
41 StringBuffer sb = new StringBuffer();
42 Random r = new Random();
43 int range = buffer.length();
44 for (int i = 0; i < length; i ++) {
45 sb.append(buffer.charAt(r.nextInt(range)));
46 }
47 return sb.toString();
48 }
49
50 }