标签:tor new while trie star current try put sys
向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 省事儿
标签:tor new while trie star current try put sys
原文地址:https://www.cnblogs.com/renjianjun/p/10868678.html