HashMap 详解七

简介:

使用 Iterator 遍历

通过 HashMap.entrySet().iterator() 方法获取迭代器, 使用 next 方法对 HashMap 进行遍历.

HashMap<String, String> map = new HashMap<>();
Iterator it = map.entrySet().iterator();
while(it.hasNext()) {
    Map.Entry<String, String> entry = it.next();
}

下面详细讲解各个方法的作用, 其实迭代器之所以能遍历元素节点, 主要是应用了内部类. 通过内部类可以访问外部类的变量和方法, 从而完成遍历节点.


entrySet()

/**
 * 直接返回 EntrySet 的实例
 * 注意这里 entrySet 不是静态方法, 而 EntrySet 是非静态的内部类, 所以可以直接 new 实例
 */
public Set<Map.Entry<K,V>> entrySet() {
    Set<Map.Entry<K,V>> es;
    return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
}

EntrySet

/**
 * EntrySet 继承于 AbstractSet
 */
final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
    ...

    /**
     * 返回 EntryIterator 实例, 这也是属于 HashMap 的非静态内部类
     */
    public final Iterator<Map.Entry<K,V>> iterator() {
        return new EntryIterator();
    }
    ...
}

EntryIterator

/**
 * HashMap 的非静态内部类
 */
final class EntryIterator extends HashIterator
        implements Iterator<Map.Entry<K,V>> {
    /**
     * next 方法调用父类 HashIterator 的 nextNode 方法, 返回下一个元素
     */
    public final Map.Entry<K,V> next() { return nextNode(); }
}

HashIterator

/**
 * HashMap 的内部抽象类
 */
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 slot

    /**
     * 构造函数, 从 0 开始遍历 HashMap 的保存数组, 一直到非空元素
     */
    HashIterator() {
        expectedModCount = modCount;
        Node<K,V>[] t = table;
        current = next = null;
        index = 0;
        if (t != null && size > 0) { // advance to first entry
            do {} while (index < t.length && (next = t[index++]) == null);
        }
    }

    public final boolean hasNext() {
        return next != null;
    }

    final Node<K,V> nextNode() {
        Node<K,V>[] t;
        Node<K,V> e = next;
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
        if (e == null)
            throw new NoSuchElementException();

        // 从根节点开始遍历链表, 其中树也当成链表结构来遍历, 一直到尾节点
        if ((next = (current = e).next) == null && (t = table) != null) {
            // 链表遍历完全后, 重新读取数组的下一个非空元素
            do {} while (index < t.length && (next = t[index++]) == null);
        }
        return e;
    }
}

以上就是 HashMap 的遍历方法, 它不是按照插入节点的先后顺序进行遍历, 而是按照数组结构来遍历.


相关文章
|
8月前
|
存储 安全 Java
HashMap详解
HashMap详解
HashMap 中的一个“坑”!(1)
HashMap 中的一个“坑”!(1)
184 0
HashMap 中的一个“坑”!(1)
|
索引
HashMap 详解一
本文代码来自JDK8 实现原理 建立一个数组 根据元素哈希值计算数组索引, 保存到数组 索引号相同的元素通过链表保存 链表长度超过范围转红黑树保存 默认常量 初始长度大小: DEFAULT_INITIAL_CAPACITY = 1 << 4, 为了区分容量和元素数目, 这里就用长度表示容量 最大长.
1164 0
|
9月前
|
Dart 算法 Java
HashMap的0.75可能只是一个经验值
HashMap的0.75可能只是一个经验值
HashMap 详解
Hashmap是一种非常常用的、应用广泛的数据类型,最近研究到相关的内容,就正好复习一下。网上关于hashmap的文章很多,但到底是自己学习的总结,就发出来跟大家一起分享,一起讨论。
783 0
|
9月前
|
存储 安全 Java
HashMap的详细解读
HashMap的详细解读
71 0
|
存储 缓存 Java
|
Java 索引 算法

热门文章

最新文章