四种遍历map方式效率比较

之前总结过三种遍历list方式的效率问题,今天看到一位博主的遍历map方式效率比较,特转载分享一下,自己也记录一下。

原文地址:https://blog.csdn.net/zajiayouzai/article/details/80922610   作者:zajiayouzai

先创建一个map,添加好数据:

Map<String, String> map = new HashMap<>();
for (int i = 0; i < 1000000; i++) {    
    map.put(i + "", i + "AA");
}

1、keySet的for循环方式:

//只获取key
public static void keySetForGetKey(Map<String, String> map){
    long startTime = System.currentTimeMillis();
    for (String key : map.keySet()) {
        }
    long endTime = System.currentTimeMillis();
    System.out.println("keySetForGetKey运行时间" + (endTime - startTime));
}

 

//获取key和value
public static void keySetForGetKeyAndValue(Map<String, String> map){
    long startTime = System.currentTimeMillis();
    for (String key : map.keySet()) {
        String value = map.get(key);
    }
    long endTime = System.currentTimeMillis();
    System.out.println("keySetForGetKeyAndValue运行时间" + (endTime - startTime));
}

2、keySet的iterator迭代器方式:

//只获取key
public static void keySetIteratorGetKey(Map<String, String> map){
    long startTime = System.currentTimeMillis();
    Iterator<String> iterator = map.keySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("keySetIteratorGetKey运行时间" + (endTime - startTime));
}
//获取key和value
public static void keySetIteratorGetKeyAndValue(Map<String, String> map){
    long startTime = System.currentTimeMillis();
    Iterator<String> iterator = map.keySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        String value = map.get(iterator.next());
    }
    long endTime = System.currentTimeMillis();
    System.out.println("keySetIteratorGetKeyAndValue运行时间" + (endTime - startTime));
}

 

3、entrySet的for循环方式:

 

//只获取key
public static void entrySetForGetKey(Map<String, String> map){
    long startTime = System.currentTimeMillis();
    for (Entry<String, String> entry : map.entrySet()) {
        String key = entry.getKey();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("entrySetForGetKey运行时间" + (endTime - startTime));
}
//获取key和value
public static void entrySetForGetKeyAndValue(Map<String, String> map){
    long startTime = System.currentTimeMillis();
    for (Entry<String, String> entry : map.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("entrySetForGetKeyAndValue运行时间" + (endTime - startTime));
}

 

4、entrySet的iterator迭代器方式:

//只获取key
public static void entrySetIteratorGetKey(Map<String, String> map){
    long startTime = System.currentTimeMillis();
    Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next().getKey();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("entrySetIteratorGetKey运行时间" + (endTime - startTime));
}
//获取key和value
public static void entrySetIteratorGetKeyAndValue(Map<String, String> map){
    long startTime = System.currentTimeMillis();
    Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
    Entry<String, String> entry;
    while (iterator.hasNext()) {
        entry = iterator.next();
        String key = entry.getKey();
        String value = entry.getValue();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("entrySetIteratorGetKeyAndValue运行时间" + (endTime - startTime));
}

 

最终的运行结果为:

keySetForGetKey运行时间28
keySetForGetKeyAndValue运行时间43
keySetIteratorGetKey运行时间25
keySetIteratorGetKeyAndValue运行时间36
entrySetForGetKey运行时间27
entrySetForGetKeyAndValue运行时间28
entrySetIteratorGetKey运行时间25
entrySetIteratorGetKeyAndValue运行时间2912345678

总结:

entrySet的方式整体都是比keySet方式要高一些;
单纯的获取key来说,两者的差别并不大,但是如果要获取value,还是entrySet的效率会更好,因为keySet需要从map中再次根据key获取value,而entrySet一次都全部获取出来;
iterator的迭代器方式比foreach的效率高。

二、foreach和iterator

其实foreach的语法只是对iterator进行了简单的包装,使用起来更加方便而已,但是如果在foreach循环体内,对集合元素进行删除添加操作的时候,会报出ConcurrentModificationException,并发修改异常。如果需要在遍历集合的时候对象集合中元素进行删除操作,需要使用iterator的遍历方式,iterator自带的remove删除方式不会报出异常。
 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值