Map和EntrySet、keySet的迭代效率
来源:互联网 发布:java url decode函数 编辑:程序博客网 时间:2024/06/01 20:48
虚拟机环境:
操作系统:win7
平台:eclipse 3.7
实验目的:对比Map和EntrySet、keySet的迭代效率,从而对比得出map效率较高的迭代方式。
实现方式:通过迭代相同的map,分别记录迭代的开始和结束时间,对比两种迭代的总耗时,得出结论。
实验结果: 10000次测试中keySet用时2999次大于entry用时
10000次测试中entry用时2323次大于keySet用时
10000次测试中用时相等4678次
实验结果:entrySet 迭代效率更高
实验代码:
public class Apple {private String name;private String id;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}}
public class Test {public static void main(String[] args) {Map<String, Apple> map = new HashMap<String, Apple>();Random random = new Random();int resultBig = 0;int resultEqul = 0;int total = 10000; for (int round = 0; round < total; round++) {int rand = random.nextInt(90000);for (int i = 0; i < rand; i++) {map.put("data" + i, new Apple());}long sumTime1 = 0;// keySet方法用时long sumTime2 = 0;// entry方法用时long startTime1 = 0;// keySet方法开始时间long startTime2 = 0;// entry方法开始时间Iterator<String> keys = map.keySet().iterator();startTime1 = System.currentTimeMillis();while (keys.hasNext()) {String key = (String) keys.next();Apple val = map.get(key);// System.out.println(key + " : " + val);}sumTime1 = System.currentTimeMillis() - startTime1;System.out.println("keySet: " + sumTime1);Iterator<Entry<String, Apple>> iter = map.entrySet().iterator();startTime2 = System.currentTimeMillis();while (iter.hasNext()) {Entry<String, Apple> entry = iter.next();String a = entry.getKey();Apple b = entry.getValue();// System.out.println(a + " : " + b);}sumTime2 = System.currentTimeMillis() - startTime2;System.out.println("entry: " + sumTime2);System.out.println("----------------"+ "map数据量: " + rand + "------------------------");if(sumTime1 > sumTime2){resultBig++;}else if(sumTime1 == sumTime2){resultEqul++;}}System.out.println(total + "次测试中" + "keySet用时" + resultBig + "次大于entry用时");System.out.println(total + "次测试中" + "entry用时" + (total-resultBig-resultEqul) + "次大于keySet用时");System.out.println(total + "次测试中" + "用时相等" + resultEqul + "次");}}
0 0
- Map和EntrySet、keySet的迭代效率
- Map.keySet 和 Map.entrySet 的效率问题
- map的keySet entrySet
- entrySet和keySet遍历Map的区别
- map中的keySet和entrySet
- JAVA MAP -keySet 和 entrySet
- HashMap的keySet遍历和entrySet遍历时间效率比较
- 关于Map遍历和Iterator的keySet和entrySet
- 遍历Map的两种方法:keySet()和entrySet()
- Map便利的两种方法:keySet和entrySet
- 遍历Map的两种方法:keySet()和entrySet()
- java基础--关于map的遍历keySet和entrySet
- 遍历Map的两种方法:keySet()和entrySet()
- Map遍历的keySet()和entrySet()性能差异原因
- 遍历Map的两种方法:keySet()和entrySet()
- Map遍历的两种方式keySet()和entrySet()
- KeySet和EntrySet的理解
- keySet和entrySet的区别
- HDU 1253 胜利大逃亡 三维BFS
- UVa-673-Parentheses Balance
- 面试逻辑思维 参考答案(二)
- LeetCode | Triangle(三角形路径和)
- Windows消息机制
- Map和EntrySet、keySet的迭代效率
- brctl配置linux bridge及虚拟bridge实现
- google的galaxy nexus没有MENU菜单键的原因,android4.0以上google不推荐使用菜单
- printf("%d",5.01)和printf("%f",5)的输出结果
- Ubuntu 12.04 IA32 编译 Jikesrvm-3.1.3
- HTML5 canvas 的一些属性
- nyoj 2 括号配对
- HDOJ 4946 Area of Mushroom(凸包)
- linux crontab设置