zoukankan      html  css  js  c++  java
  • map遍历

    向map中添加数据 150万
    Map<String,String> m = new HashMap<>(); for (int i = 0; i < 1500000; i++) { m.put("a" + i,"1"); } long start2 = System.currentTimeMillis(); Set<Map.Entry<String, String>> entries1 = m.entrySet(); Iterator<Map.Entry<String, String>> iterator = entries1.iterator(); while (iterator.hasNext()){ Map.Entry<String, String> next = iterator.next(); System.out.println(next.getKey() + next.getValue()); } long iteratorTime = System.currentTimeMillis() - start2; long start = System.currentTimeMillis();for (Map.Entry<String, String> entry : m.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } long entrySetTime = System.currentTimeMillis() - start; long start1 = System.currentTimeMillis(); for (String s : m.keySet()) { System.out.println(s + " " + m.get(s)); } long keySetTime = System.currentTimeMillis() - start1; System.out.println("iteratorTime" + iteratorTime); System.out.println("entrySetTime" + entrySetTime); System.out.println("keySetTime" + keySetTime);


    第一次运行的结果:

      iteratorTime5556
      entrySetTime5620
      keySetTime5779

     第二次运行结果

      iteratorTime5194
      entrySetTime5267
      keySetTime5296

    再运行几次 

    iteratorTime5465
    entrySetTime5594
    keySetTime5703

    map数据是10万左右的时候运行多次,结果就不一样了,三个都有最快的时候,不知道为什么,但是从数据最多的情况来分析,还是iterator方式遍历的速度是最快的。

    所以,我们使用中为了速度,我们使用iterator

    写代码简单

    keySet和entrySet 省事儿
  • 相关阅读:
    VS2010中添加MVC3和MVC4
    C#--对象转成XML的方法
    Web优化的措施
    Socket的使用(简单测试)
    WCF、WebAPI、WCFREST、WebService之间的区别
    Java和C#(笔记)
    各种窗口最小化快捷键详解
    SASS的安装及使用(前提:安装Ruby)
    查看Linux是32位还是64位
    log4j输出日志到文件
  • 原文地址:https://www.cnblogs.com/renjianjun/p/10868678.html
Copyright © 2011-2022 走看看