首发于本人的CSDN:点这访问 ( https://blog.csdn.net/weixin_43438052/article/details/113919663 )
测试用例
//HashMap1:大小为1000000,
//key和value的值均为String,
//key的值为1、2、3.........1000000
Map<String,String> map1 =new HashMap<String,String>();
String key1,value1;
for(int i=1;i<=1000000;i++){
key1 = ""+i;
value1="value"+i;
map1.put(key1,value1);
}
//HashMap2:大小为1000000,
//key和value的值为String,
//key的值为50、100、150........50000000
Map<String,String> map2 = new HashMap<String,String>();
String key2,value2;
for(int i=1;i<=1000000;i++){
key2=""+(i*50);
value2="value"+key2;
map2.put(key2,value2);
}
测试场景
同时遍历 key+value
keySet利用Iterator遍历
public static long keySet_Iterator(Map<String, String> map) {
long startTime1 =System.currentTimeMillis();
Iterator<String> iter = map.keySet().iterator();
while (iter.hasNext()){
key=iter.next();
value=map.get(key);
}
long endTime1 =System.currentTimeMillis();
return endTime1-startTime1;
}
keySet利用for遍历
public static long keySet_for(Map<String, String> map) {
long startTime2 =System.currentTimeMillis();
for(String key3:map.keySet()){
value=map.get(key3);
}
long endTime2 =System.currentTimeMillis();
return endTime2-startTime2;
}
entrySet利用Iterator遍历
public static long entrySet_Iterator(Map<String, String> map) {
long startTime3=System.currentTimeMillis();
Iterator<Map.Entry<String,String>> iter3 =map.entrySet().iterator();
Map.Entry<String,String> entry3;
while (iter3.hasNext()){
entry3 = iter3.next();
key = entry3.getKey();
value=entry3.getValue();
}
long endTime3 =System.currentTimeMillis();
return endTime3-startTime3;
}
entrySet利用for遍历
public static long entrySet_for(Map<String, String> map) {
long startTime4=System.currentTimeMillis();
for(Map.Entry<String,String> entry4:map.entrySet()){
key=entry4.getKey();
value=entry4.getValue();
}
long endTime4 =System.currentTimeMillis();
return endTime4-startTime4;
}
只遍历 key
keySet利用Iterator遍历
public static long keySet_Iterator(Map<String, String> map) {
long startTime1 =System.currentTimeMillis();
Iterator<String> iter = map.keySet().iterator();
while (iter.hasNext()){
key=iter.next();
}
long endTime1 =System.currentTimeMillis();
return endTime1-startTime1;
}
keySet利用for遍历
public static long keySet_for(Map<String, String> map) {
long startTime2 =System.currentTimeMillis();
for(String key3:map.keySet()){
}
long endTime2 =System.currentTimeMillis();
return endTime2-startTime2;
}
entrySet利用Iterator遍历
public static long entrySet_Iterator(Map<String, String> map) {
long startTime3=System.currentTimeMillis();
Iterator<Map.Entry<String,String>> iter3 =map.entrySet().iterator();
Map.Entry<String,String> entry3;
while (iter3.hasNext()){
key = iter3.next().getKey();
}
long endTime3 =System.currentTimeMillis();
return endTime3-startTime3;
}
entrySet利用for遍历
public static long entrySet_for(Map<String, String> map) {
long startTime4=System.currentTimeMillis();
for(Map.Entry<String,String> entry4:map.entrySet()){
key=entry4.getKey();
}
long endTime4 =System.currentTimeMillis();
return endTime4-startTime4;
}
只遍历 value
keySet利用Iterator遍历
public static long keySet_Iterator(Map<String, String> map) {
long startTime1 =System.currentTimeMillis();
Iterator<String> iter = map.keySet().iterator();
while (iter.hasNext()){
value=map.get(iter.next());
}
long endTime1 =System.currentTimeMillis();
return endTime1-startTime1;
}
keySet利用for遍历
public static long keySet_for(Map<String, String> map) {
long startTime2 =System.currentTimeMillis();
for(String key3:map.keySet()){
value=map.get(key2);
}
long endTime2 =System.currentTimeMillis();
return endTime2-startTime2;
}
entrySet利用Iterator遍历
public static long entrySet_Iterator(Map<String, String> map) {
long startTime3=System.currentTimeMillis();
Iterator<Map.Entry<String,String>> iter3 =map.entrySet().iterator();
Map.Entry<String,String> entry3;
while (iter3.hasNext()){
value=iter3.next().getValue();
}
long endTime3 =System.currentTimeMillis();
return endTime3-startTime3;
}
entrySet利用for遍历
public static long entrySet_for(Map<String, String> map) {
long startTime4=System.currentTimeMillis();
for(Map.Entry<String,String> entry4:map.entrySet()){
value=entry4.getValue();
}
long endTime4 =System.currentTimeMillis();
return endTime4-startTime4;
}
values利用iterator遍历
public static values_Iterator(Map<String, String> map) {
long startTime5=System.currentTimeMillis();
Iterator<String> iter5=map.values().iterator();
while (iter5.hasNext()){
value=iter5.next();
}
long endTime5 =System.currentTimeMillis();
return endTime5-startTime5;
}
values利用for遍历
public static values_for(Map<String, String> map) {
long startTime6=System.currentTimeMillis();
for(String value6:map.values()){
}
long endTime6 =System.currentTimeMillis();
return endTime6-startTime6;
}
测试结果
同时遍历 key+value
同时遍历 key+value |
数据量小 |
数据量大 |
keySet利用Iterator遍历 |
124ms |
126ms |
keySet利用for遍历 |
105ms |
113ms |
entrySet利用Iterator遍历 |
80ms |
100ms |
entrySet利用for遍历 |
78ms |
103ms |
只遍历 key
只遍历 key |
数据量小 |
数据量大 |
keySet利用Iterator遍历 |
97ms |
118ms |
keySet利用for遍历 |
93ms |
114ms |
entrySet利用Iterator遍历 |
95ms |
115ms |
entrySet利用for遍历 |
95ms |
113ms |
只遍历 value
只遍历 value |
数据量小 |
数据量大 |
keySet利用Iterator遍历 |
125ms |
146ms |
keySet利用for遍历 |
114ms |
140ms |
entrySet利用Iterator遍历 |
102ms |
120ms |
entrySet利用for遍历 |
104ms |
121ms |
values利用iterator遍历 |
97ms |
124ms |
values利用for遍历 |
107ms |
114ms |
小结
- map的key采用简单形式和复杂形式时,查找的效率是不同的,简单的key值效率更高
- 当数据量大的时候,采用entrySet遍历key+value的效率要高于keySet
这也是为什么“大家建议使用entrySet,认为entrySet对于大数据量的查找来说,速度更快”
- 当我们只需要取得value值时,采用values来遍历效率更高