map遍历方式及效率_java遍历map集合

map遍历方式及效率_java遍历map集合一、map遍历的效率先创建一个map,添加好数据:Map<String,String>map=newHashMap<>();for(inti=0;i<1000000;i++){map.put(i+"",i+"AA");}1、keySet的for循环方式://只获取keypublicstaticv…


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

一、map遍历的效率

先创建一个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();
    while (iterator.hasNext()) {
        String key = iterator.next().getKey();
        String value = iterator.next().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运行时间29

总结:

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

二、foreach和iterator

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191039.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • UltraEdit 配置 shell 语法高亮

    UltraEdit 配置 shell 语法高亮从UE官网下载wordfile扩展文件包,http://www.ultraedit.com/files/wf/wf.zip,解压后找到文件“unixshell.uew”,将其复制到ue的wordfile配置文件夹.点击菜单上的“高级”–>“配置”勾选“启用语法彩色显示”–>“已安装的词语文”选择bash–>点击应用,重启UE即可本文参考:http://blog.cs

    2022年7月15日
  • eureka集群高可用配置[通俗易懂]

    eureka集群高可用配置[通俗易懂]网上讲这个东西的很多,抄来抄去的,大部分类似,多数没讲明白为什么那么配置。譬如eureka.client.register-with-eureka和fetch-registry是否要配置,配不配区别在哪里;eureka的客户端添加service-url时,是不是需要把所有的eureka的server地址都写上,还是只需要写一个server就可以了(因为server之间已经相互注册了)?如果写上了所…

    2022年6月14日
  • pycharm加注释的快捷方式_pycharm如何批量注释

    pycharm加注释的快捷方式_pycharm如何批量注释1、主题  在开发项目的过程中经常需要创建任务列表,对于一些小任务,在代码中插入便签注释会有奇效。  这种注释的格式如下:  2、创建便签注释  非常简单:  首先,将光标定位到相应行。  然后,创建一行注释(Ctrl+Slash)。  最后,在#后面,输入TODO或者todo,然后输入注释  3、浏览便签  在T

    2022年8月28日
  • JDBC batch批处理Statement executeBatch 具体解释

    JDBC batch批处理Statement executeBatch 具体解释

    2021年11月30日
  • intellij idea 2021激活码(注册激活)

    (intellij idea 2021激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~1STL…

    2022年3月27日
  • PyCharm激活码永久有效PyCharm2021.2.3激活码教程-持续更新,一步到位「建议收藏」

    PyCharm激活码永久有效PyCharm2021.2.3激活码教程-持续更新,一步到位「建议收藏」PyCharm激活码永久有效2021.2.3激活码教程-Windows版永久激活-持续更新,Idea激活码2021.2.3成功激活

    2022年6月19日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号