Map.keySet 和 Map.entrySet 的效率问题
来源:互联网 发布:乌斯玛草 知乎 编辑:程序博客网 时间:2024/06/05 21:50
import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class Test { public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); for(int i=0;i<10000;i++){ map.put(i+"", i); } long begin1 = System.currentTimeMillis(); Set<String> keyset = map.keySet(); Iterator<String> it = keyset.iterator(); while(it.hasNext()){ String k = it.next(); System.out.println(map.get(k)); } long end1 = System.currentTimeMillis(); long begin2 = System.currentTimeMillis(); Set<Map.Entry<String, Integer>> entries = map.entrySet(); Iterator<Map.Entry<String, Integer>> it2 = entries.iterator(); while(it2.hasNext()){ Map.Entry<String, Integer> next = it2.next(); System.out.println(next.getKey()+"---"+next.getValue()); } long end2 = System.currentTimeMillis(); System.out.println((end1-begin1)+"---"+(end2-begin2));; }}
执行时间,用keySet方式遍历要比entrySet的方式用时大两倍!
时间差在,keySet方式要再从Map里面获取一遍。
1 0
- Map.keySet 和 Map.entrySet 的效率问题
- Map和EntrySet、keySet的迭代效率
- map的keySet entrySet
- entrySet和keySet遍历Map的区别
- map中的keySet和entrySet
- JAVA MAP -keySet 和 entrySet
- Map.Entry和Map中的map.keySet()、map.entrySet()详解
- 关于Map遍历和Iterator的keySet和entrySet
- Map---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()
- Map中entrySet()和keySet()比较
- 慕课网视频资源下载方法
- class.forName的官方用法说明
- android fragment activity详解
- Bootstrap学习--CSS
- hdu 5326 work 搜索 2015多校联合训练赛
- Map.keySet 和 Map.entrySet 的效率问题
- Linux 下安装phpredis
- 高阶函数,集合
- eclipse点击后出现exit code=13
- 马拦过河卒
- Codeforces 3C
- 小白学开发(iOS)OC_new方法原理(2015-07-29)
- android Socket 编程
- Opencv常见用法和常见错误(一)