HashMap 源码分析 -- entrySet()
来源:互联网 发布:家电维修软件 编辑:程序博客网 时间:2024/05/19 10:35
HashMap extends AbstractMap implements Map
AbstractMap implements Map
AbstractMap已经实现了map里的一些公共的接口,比如
size(),isEmpty() 等。在AbstractMap里留下了一个abstract 方法
public abstract Set<Entry<K,V>> entrySet()
HashMap 实现只是简单的new EntrySet
public Set<Map.Entry<K,V>> entrySet() { Set<Map.Entry<K,V>> es; return (es = entrySet) == null ? (entrySet = new EntrySet()) : es; }
HashMap的成员类-EntrySet
final class EntrySet extends AbstractSet<Map.Entry<K,V>> { public final int size(){ return size; } public final void clear() { HashMap.this.clear(); } //构造函数里new EntryIterator() public final Iterator<Map.Entry<K,V>> iterator() { return new EntryIterator(); } ...}
HashMap的又一个成员类-EntryIterator extends HashIterator
final class EntryIterator extends HashIterator implements Iterator<Map.Entry<K,V>> { public final Map.Entry<K,V> next() { return nextNode(); } }
HashMap里定义的abstract class HashIterator
abstract class HashIterator { Node<K,V> next; // next entry to return Node<K,V> current; // current entry int expectedModCount; // for fast-fail int index; // current slotHashIterator() { expectedModCount = modCount; //table 是HashMap里的数组容器,很关键的一步 //transient Node<K,V>[] table; Node<K,V>[] t = table; current = next = null; index = 0; if (t != null && size > 0) { //直到table[index]不为空,将 table[index]付给next //也就是next是Node<K,V> //Node<K,V> implements Map.Entry<K,V> //因此下面的Iterator<Map.Entry<String,String>>里的 //泛型是Map.Entry<String,String> do {} while (index < t.length && (next = t[index++]) == null); } } ....}
调用map的entrySet()来遍历
Map<String, String> map = new HashMap<String, String>(); map.put("1", "1"); map.put("2", "2"); map.put("3", "3"); Set<Map.Entry<String, String>> mapSet = map.entrySet(); Iterator<Map.Entry<String,String>> it = mapSet.iterator(); while(it.hasNext()){ Map.Entry<String,String> entry = it.next(); System.out.println(entry.getKey() + " = " +entry.getValue()); }
阅读全文
0 0
- HashMap 源码分析 -- entrySet()
- HashMap EntrySet源码 分析(修改版)
- 简单分析hashmap 中的entryset()方法
- HashMap之entrySet( )底层实现原理分析
- 使用entrySet遍历HashMap
- 使用entrySet遍历HashMap
- HashMap的entrySet()方法
- 源码分析:HashMap
- 源码分析:HashMap
- HashMap源码分析
- HashMap 源码分析
- HashMap源码分析
- HashMap LinkedHashMap源码分析
- HashMap源码分析
- HashMap 源码分析
- HashMap源码分析
- HashMap源码分析
- HashMap源码分析
- jdbc学习
- 设计模式--策略模式
- PAT (Basic Level) Practise (中文)1062. 最简分数(20)
- 吐槽天猫精灵链接不上网络
- 猜拳游戏
- HashMap 源码分析 -- entrySet()
- springcloud和dubbo微服务架构图
- python学习笔记——字符串操作
- 深夜沐月幻想乡——逝月镜夜【前篇】
- Spring Security 中实现 Remember Me 记住密码功能
- 优雅的解决SwipeRefreshLayout和ListView的EmptyView共存冲突的问题(全网独创)
- 去掉控制台程序的黑框框
- Swift4.0实现简单通讯录功能,FMDB框架实现联系人的增删改查
- 分析C语言声明