再来谈谈HashMap中的entrySet
来源:互联网 发布:淘宝宝贝优化教程 编辑:程序博客网 时间:2024/05/17 22:19
HashMap,可能是java中应用较多的Collection之一,之前大概看过一次其中的原理,无非就是先用key的hashcode查找桶,然后用equals比较具体的对象。但是最近在遍历HashMap的时候,发现有个entrySet(),仔细看了源码之后才发现其中的奥秘(当然keySet和ValueSet也是类似的原理,之前还以为keySet是HashMap中的一个副本,囧)
在说EntrySet之前,先看看HashMap的另外一个内部类
private abstract class HashIterator<E> implements Iterator<E> { Entry<K,V> next; // next entry to return int expectedModCount; // For fast-fail int index; // current slot Entry<K,V> current; // current entry HashIterator() { expectedModCount = modCount; if (size > 0) { // advance to first entry Entry[] t = table; while (index < t.length && (next = t[index++]) == null) ; } } public final boolean hasNext() { return next != null; } final Entry<K,V> nextEntry() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); Entry<K,V> e = next; if (e == null) throw new NoSuchElementException(); if ((next = e.next) == null) { Entry[] t = table; while (index < t.length && (next = t[index++]) == null) ; } current = e; return e; } public void remove() { if (current == null) throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); Object k = current.key; current = null; HashMap.this.removeEntryForKey(k); expectedModCount = modCount; } }由于这个方法没有next,所以还是一个抽象类,然后具体用子类实现,比如EntrySet需要用EntryIterator,只要实现next()方法就可以了。对应的KeyIteraror和ValueInteraror也是类似
private final class EntryIterator extends HashIterator<Map.Entry<K,V>> { public Map.Entry<K,V> next() { return nextEntry(); } }
private final class ValueIterator extends HashIterator<V> { public V next() { return nextEntry().value; } } private final class KeyIterator extends HashIterator<K> { public K next() { return nextEntry().getKey(); } }
好了,为什么需要用到这些iterator呢? 其实就是后面EntrySet用来的,具体的作用了,就是用iterator来搭起HashMap中具体的entry和entrySet中的桥梁
private final class EntrySet extends AbstractSet<Map.Entry<K,V>> { public Iterator<Map.Entry<K,V>> iterator() { return newEntryIterator(); } public boolean contains(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry<K,V> e = (Map.Entry<K,V>) o; Entry<K,V> candidate = getEntry(e.getKey()); return candidate != null && candidate.equals(e); } public boolean remove(Object o) { return removeMapping(o) != null; } public int size() { return size; } public void clear() { HashMap.this.clear(); } }
这样,对于entrySet的remove方法会体现在HashMap中entry数组(也就是 removeMapping()方法),对entrySet中的clear方法也会影响到HashMap中。(以上情况反之亦然)
注意:1、但是entrySet没有实现add方法,
2、clear方法中 HashMap.this.clear() 是调用HashMap对象中的clear
0 1
- 再来谈谈HashMap中的entrySet
- HashMap中的keySet()和entrySet()
- 简单分析hashmap 中的entryset()方法
- HashMap 中的keySet()和entrySet()方法的比较
- 再来谈谈json
- 再来谈谈vue.js
- 使用entrySet遍历HashMap
- 使用entrySet遍历HashMap
- HashMap的entrySet()方法
- HashMap 源码分析 -- entrySet()
- HashMap的entrySet与keySet
- HashMap的entrySet与keySet
- 这一年,我们再来谈谈开源?
- Android单元测试(二):再来谈谈为什么
- Android单元测试(二):再来谈谈为什么
- keySet 与entrySet 遍历HashMap性能差别
- entrySet最高效的遍历hashmap
- keySet 与entrySet 遍历HashMap性能差别
- VI粘贴板-寄存器对象的图示
- 解决oc中中文乱码问题
- Linux内核工程导论——Linux的启动
- BZOJ1059
- 通过包名判断手机上是否安装了QQ,微信,或者其他应用
- 再来谈谈HashMap中的entrySet
- Spark Core and Cluster Managers(翻译自Learning.Spark.Lightning-Fast.Big.Data.Analysis)
- Android如何自定义Toast?and 四种AleterDialog的用法
- jquery 自定义插件开发整理
- JavaScript 闭包
- android学习——MeasureSpec介绍及使用
- 使用CSS实现DIV成半透明效果,CSS实现层与背景半透明效果。
- ACdream 1216 Beautiful People(二路最长上升子序列 O(nlogn) )
- MongoDB学习九--MongoDB的主从复制