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 的遍历方法, 它不是按照插入节点的先后顺序进行遍历, 而是按照数组结构来遍历.


相关文章
|
3月前
|
存储 安全 Java
HashMap的详细解读
HashMap的详细解读
48 0
|
3月前
|
存储 算法 索引
|
存储 缓存 Java
|
存储 算法 安全
【HashMap】
【HashMap】
118 0
|
存储 安全 Oracle
HashMap你真的了解吗?
HashMap你真的了解吗?
103 0
HashMap你真的了解吗?
|
存储 安全 算法
再聊 HashMap
HashMap特点: KV 结构,K、V 都允许 null 值; 线程不安全,运行速度快,存取速度快; 非线程安全的
再聊 HashMap
HashMap 中的一个“坑”!(3)
HashMap 中的一个“坑”!(3)
202 0
HashMap 中的一个“坑”!(3)
|
索引
HashMap 详解一
本文代码来自JDK8 实现原理 建立一个数组 根据元素哈希值计算数组索引, 保存到数组 索引号相同的元素通过链表保存 链表长度超过范围转红黑树保存 默认常量 初始长度大小: DEFAULT_INITIAL_CAPACITY = 1 << 4, 为了区分容量和元素数目, 这里就用长度表示容量 最大长.
1146 0
HashMap 详解五
红黑树性质 红黑树是平衡二叉树的一种, 但是它的平衡因子是可以大于 1 红黑树的节点要么是红色, 要么是黑色, 这里的红黑色只是用来区分的一种方式, 为了定义规则 根节点一定是黑色 叶子节点也是黑色, 实际上叶子节点都是由 NULL 组成 红色节点的子节点是黑色 根节点到叶子节点的路径都.
1069 0