使用 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 的周遊方法, 它不是按照插入節點的先後順序進行周遊, 而是按照數組結構來周遊.