keySet 与entrySet 遍历HashMap性能差别
博客分类: Java
一.问题发现
今天,在写完代码后用Find Bugs扫锚了一下,发现类中一处代码中有提示如下内容:
Java代码
Map<String, EventChain> map = ContextHolder.getContext().getEventChains();
for (Iterator<String> iter = map.keySet().iterator(); iter.hasNext();) {
String key = iter.next();
EventChain eventChain = eventChains.get(key);
}
Map<String, EventChain> map = ContextHolder.getContext().getEventChains();
for (Iterator<String> iter = map.keySet().iterator(); iter.hasNext();) {
String key = iter.next();
EventChain eventChain = eventChains.get(key);
}
makes inefficient use of keySet iterator instead of entrySet iterator
意思是说用keySet 方式遍历Map的性能不如entrySet性能好.起初想不明白,索性仔细看下代码:
二.常用的遍历HashMap的两种方法
1.第一种方式
Java代码
Iterator<String> keySetIterator = keySetMap.keySet().iterator();
while (keySetIterator.hasNext()) {
String key = keySetIterator.next();
String value = keySetMap.get(key);
}
Iterator<String> keySetIterator = keySetMap.keySet().iterator();
while (keySetIterator.hasNext()) {
String key = keySetIterator.next();
String value = keySetMap.get(key);
}
2.第二种方式
Java代码
Iterator<Entry<String, String>> entryKeyIterator = entrySetMap.entrySet()
.iterator();
while (entryKeyIterator.hasNext()) {
Entry<String, String> e = entryKeyIterator.next();
String value=e.getValue();
}
Iterator<Entry<String, String>> entryKeyIterator = entrySetMap.entrySet()
.iterator();
while (entryKeyIterator.hasNext()) {
Entry<String, String> e = entryKeyIterator.next();
String value=e.getValue();
}
三.性能比较
到底第二种方式的性能比第一种方式的性能高多少呢,通过一个简单的测试类可以看一下,测试代码如下:
Java代码
public class HashMapTest {
public static void main(String[] args) {
HashMap<String, String> keySetMap = new HashMap<String, String>();
HashMap<String, String> entrySetMap = new HashMap<String, String>();
for (int i = 0; i < 1000; i++) {
keySetMap.put("" + i, "keySet");
}
for (int i = 0; i < 1000; i++) {
entrySetMap.put("" + i, "entrySet");
}
long startTimeOne = System.currentTimeMillis();
Iterator<String> keySetIterator = keySetMap.keySet().iterator();
while (keySetIterator.hasNext()) {
String key = keySetIterator.next();
String value = keySetMap.get(key);
System.out.println(value);
}
System.out.println("keyset spent times:"
+ (System.currentTimeMillis() - startTimeOne));
long startTimeTwo = System.currentTimeMillis();
Iterator<Entry<String, String>> entryKeyIterator = entrySetMap
.entrySet().iterator();
while (entryKeyIterator.hasNext()) {
Entry<String, String> e = entryKeyIterator.next();
System.out.println(e.getValue());
}
System.out.println("entrySet spent times:"
+ (System.currentTimeMillis() - startTimeTwo));
}
}
public class HashMapTest {
public static void main(String[] args) {
HashMap<String, String> keySetMap = new HashMap<String, String>();
HashMap<String, String> entrySetMap = new HashMap<String, String>();
for (int i = 0; i < 1000; i++) {
keySetMap.put("" + i, "keySet");
}
for (int i = 0; i < 1000; i++) {
entrySetMap.put("" + i, "entrySet");
}
long startTimeOne = System.currentTimeMillis();
Iterator<String> keySetIterator = keySetMap.keySet().iterator();
while (keySetIterator.hasNext()) {
String key = keySetIterator.next();
String value = keySetMap.get(key);
System.out.println(value);
}
System.out.println("keyset spent times:"
+ (System.currentTimeMillis() - startTimeOne));
long startTimeTwo = System.currentTimeMillis();
Iterator<Entry<String, String>> entryKeyIterator = entrySetMap
.entrySet().iterator();
while (entryKeyIterator.hasNext()) {
Entry<String, String> e = entryKeyIterator.next();
System.out.println(e.getValue());
}
System.out.println("entrySet spent times:"
+ (System.currentTimeMillis() - startTimeTwo));
}
}
通过测试发现,第二种方式的性能通常要比第一种方式高一倍.
四.原因分析:
通过查看源代码发现,调用这个方法keySetMap.keySet()会生成KeyIterator迭代器,其next方法只返回其key值.
Java代码
private class KeyIterator extends HashIterator<K> {
public K next() {
return nextEntry().getKey();
}
}
private class KeyIterator extends HashIterator<K> {
public K next() {
return nextEntry().getKey();
}
}
而调用entrySetMap.entrySet()方法会生成EntryIterator 迭代器,其next方法返回一个Entry对象的一个实例,其中包含key和value.
Java代码
private class EntryIterator extends HashIterator<Map.Entry<K,V>> {
public Map.Entry<K,V> next() {
return nextEntry();
}
}
private class EntryIterator extends HashIterator<Map.Entry<K,V>> {
public Map.Entry<K,V> next() {
return nextEntry();
}
}
二者在此时的性能应该是相同的,但方式一再取得key所对应的value时,此时还要访问Map的这个方法,这时,方式一多遍历了一次table.
Java代码
public V get(Object key) {
Object k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length);
Entry<K,V> e = table[i];
while (true) {
if (e == null)
return null;
if (e.hash == hash && eq(k, e.key))
return e.value;
e = e.next;
}
}
public V get(Object key) {
Object k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length);
Entry<K,V> e = table[i];
while (true) {
if (e == null)
return null;
if (e.hash == hash && eq(k, e.key))
return e.value;
e = e.next;
}
}
这个方法就是二者性能差别的主要原因.
分享到:
相关推荐
可以通过2种方法遍历HashMap <br>Map map = new HashMap(); <br>for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { <br> Map.Entry entry = (Map.Entry) iter.next(); <br> Object ...
1、遍历Map.entrySet():它的每一个元素都是Map.Entry对象,这个对象中, 放着的就是Map中的某一对key-value; 2、遍历Map.keySet():它是Map中key值的集合,我们可以通过遍历这个集合来 读取Map中的元素; 3、...
看完这篇 HashMap,和面试官扯皮就没问题了 - HashMap 概述 - HashMap 和 HashTable 的区别 - 相同点 - 不同点 - HashMap 和 HashSet 的... - HashMap 的遍历方式 - HashMap 中的移除方法 - 关于 HashMap 的面
// hashmap entrySet() 遍历 for(Map.Entry<Object,Object> m: hash.entrySet()){ System.out.println(m.getKey()+"---"+m.getValue()); } //hashmap keySet() 遍历 for(Object m: hash....
java,利用keySet进行遍历map
遍历MAP的几种方法,利用keyset进行遍历,它的优点在于可以根据你所想要的key值得到你想要的 values,更具灵活性
简单地说,在keyset方法返回的set上做修改会改变原来hashmap,这也许不是你想要的,于是形成一个隐藏的bug
对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多。理由是:entrySet方法一次拿到所有key和value的集合;而keySet拿到的只是key的集合,针对每个key,都要去Map中额外查找一次...
此文件是用eclipse打包的,如果用eclipse工具可直接导入查看,如果用的是idea工具,可以打开bin下的.class文件之后,拖入到idea运行即可
DoubleAccessMap 可以通过使用值访问KeySet的HashMap
今天小编就为大家分享一篇关于Java源码解析HashMap的keySet()方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
Java HashMap的插图 Java HashMap HashMap类使用哈希表来实现Map接口。 这样,即使对于大型集合,诸如get()和put()之类的基本操作的执行时间也可以保持恒定。 目录 插图1:使用put()方法在HashMap中创建和...
python库。 资源全名:django-keyset-pagination-plus-0.9.9.tar.gz
HDCP KSV和keyset对应关系介绍HDCP KSV和keyset对应关系介绍HDCP KSV和keyset对应关系介绍HDCP KSV和keyset对应关系介绍HDCP KSV和keyset对应关系介绍HDCP KSV和keyset对应关系介绍HDCP KSV和keyset对应关系介绍HDCP...
java中的map遍历有多种方法,从早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点。 先初始化一个map public class TestMap { public static Map<Integer>...
因此通常建议能提前预估 HashMap 的大小最好,尽量的减少扩容带来的性能损耗。 根据代码可以看到其实真正存放数据的是 transient Entry,V>[] table = (Entry,V>[]) EMPTY_TABLE; 这个数组,那么它又是如何定义的...
NULL 博文链接:https://hoochiang.iteye.com/blog/1816970
java学习必看,适合初学者辅助学习。集合中常用的是:ArrayList,HashSet,HashMap。其中ArrayList和HashMap...遍历Map,使用keySet()可以返回set值,用keySet()得到key值,使用迭代器遍历,然后使用put()得到value值。
主要为大家详细介绍了Java使用keySet方法获取Map集合中的元素,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在用户界面方面,键集分页与无限滚动元素配合得很好。 键集分页可提供稳定的结果。 下一批记录总是从上一批的最后一条记录开始,即使查询之间会有插入或删除。 在SQL 中使用Objection.js 自己的.page()或OFFSET等...