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的详细解读
31 0
|
2月前
|
Dart 算法 Java
HashMap的0.75可能只是一个经验值
HashMap的0.75可能只是一个经验值
|
10月前
|
存储 算法
详解HashMap
1.hash code hash code是使用hash函数运算得到的一个值,是对象的身份证号码,用于对象的辨重。在同一运行周期,对同一个对象多次调用hashcode(),只要是用于equals()的内容未变,那么每次得到的hash码也应该不变。,但不同运行周期间hash码可能会不同。
83 0
|
存储 算法 安全
【HashMap】
【HashMap】
100 0
|
存储 安全 Oracle
HashMap你真的了解吗?
HashMap你真的了解吗?
90 0
HashMap你真的了解吗?
|
存储 安全 算法
再聊 HashMap
HashMap特点: KV 结构,K、V 都允许 null 值; 线程不安全,运行速度快,存取速度快; 非线程安全的
再聊 HashMap
|
安全 算法 数据挖掘
厉害了!把 HashMap 剖析的只剩渣了!
很高兴遇见你~ HashMap是一个非常重要的集合,日常使用也非常的频繁,同时也是面试重点。本文并不打算讲解基础的使用api,而是深入HashM
厉害了!把 HashMap 剖析的只剩渣了!
|
存储
HashMap 中的一个“坑”!(2)
HashMap 中的一个“坑”!(2)
185 0
HashMap 中的一个“坑”!(2)
HashMap 中的一个“坑”!(3)
HashMap 中的一个“坑”!(3)
180 0
HashMap 中的一个“坑”!(3)
|
存储 机器学习/深度学习 算法
HashMap详解
HashMap详解
2700 0
HashMap详解

热门文章

最新文章