Map 遍历的几种方式和性能分析

Map遍历方式主要有entrySet和keySet,还可以用迭代器 Iterator

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Demo {

public static void main(String[] args){
Map<String, Object> map = getMap();
keySet(map);
keySetIterator(map);
entrySet(map);
entrySetIterator(map);
java8(map);
}

public static Map getMap(){
Map<String, Object> map = new HashMap<String, Object>();
for(int i = 0;i<1000000; i++){
map.put("A"+i,i);
}
return map;
}
public static void keySet(Map<String,Object> map){
long startTime = DateUtil.currentTimeMilliSeconds();
int count = 0;
for(String key : map.keySet()){
count++;
String value = map.get(key).toString();
}
long endTime = System.currentTimeMillis();
System.out.println("keySetGetKeyAndValue运行时间" + (endTime - startTime)+"----"+count);
}
public static void keySetIterator(Map<String,Object> map){
long startTime = DateUtil.currentTimeMilliSeconds();
Iterator<String> iterator = map.keySet().iterator();
int count = 0;
while(iterator.hasNext()){
count++;
String key = iterator.next();
String value = map.get(key).toString();
}
long endTime = System.currentTimeMillis();
System.out.println("keySetIteratorGetKeyAndValue运行时间" + (endTime - startTime)+"---------"+count);
}
public static void entrySet(Map<String,Object> map){
long startTime = DateUtil.currentTimeMilliSeconds();
int count = 0;
for(Map.Entry<String,Object> entry: map.entrySet()){
count++;
String key = entry.getKey();
String value = entry.getValue().toString();
}
long endTime = System.currentTimeMillis();
System.out.println("entrySetGetKeyAndValue运行时间" + (endTime - startTime)+"-----"+count);
}
public static void entrySetIterator(Map<String,Object> map){
long startTime = DateUtil.currentTimeMilliSeconds();
Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
int count = 0;
while (iterator.hasNext()) {
count++;
Map.Entry<String,Object> entry = iterator.next();
String key = entry.getKey();
String value = entry.getValue().toString();
}
long endTime = System.currentTimeMillis();
System.out.println("entrySetIteratorGetKeyAndValue运行时间" + (endTime - startTime)+"---"+count);
}
public static void java8(Map<String,Object> map){
long startTime = DateUtil.currentTimeMilliSeconds();
map.forEach((key, value) -> {
// String key1 = key;
// String value1 = value.toString();
});
long endTime = System.currentTimeMillis();
System.out.println("java8内置方法运行时间" + (endTime - startTime));
}
}
100万条同时得到key和value所用的时间

keySetGetKeyAndValue运行时间75----1000000
keySetIteratorGetKeyAndValue运行时间82---------1000000
entrySetGetKeyAndValue运行时间96-----1000000
entrySetIteratorGetKeyAndValue运行时间66---1000000
java8内置方法运行时间62

只用到key时

keySetGetKeyAndValue运行时间25----1000000
keySetIteratorGetKeyAndValue运行时间22---------1000000
entrySetGetKeyAndValue运行时间25-----1000000
entrySetIteratorGetKeyAndValue运行时间24---1000000

值用到value时

keySetGetKeyAndValue运行时间79----1000000
keySetIteratorGetKeyAndValue运行时间81---------1000000
entrySetGetKeyAndValue运行时间92-----1000000
entrySetIteratorGetKeyAndValue运行时间67---1000000
java8内置方法运行时间57



原文地址:https://www.cnblogs.com/echo777/p/11799833.html