abstractclassHashIterator { Node<K,V> next; // next entry to return Node<K,V> current; // current entry int expectedModCount; // for fast-fail int index; // current slot
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); } }
publicfinalbooleanhasNext() { return next != null; }
final Node<K,V> nextNode() { Node<K,V>[] t; Node<K,V> e = next; // 这里判断 modCount 和 expectedModCount 是否一致,若不一致就抛出 if (modCount != expectedModCount) thrownewConcurrentModificationException(); if (e == null) thrownewNoSuchElementException(); if ((next = (current = e).next) == null && (t = table) != null) { do {} while (index < t.length && (next = t[index++]) == null); } return e; }
publicfinalvoidremove() { Node<K,V> p = current; if (p == null) thrownewIllegalStateException(); // 同上 if (modCount != expectedModCount) thrownewConcurrentModificationException(); current = null; Kkey= p.key; removeNode(hash(key), key, null, false, false); expectedModCount = modCount; } }