HashMap的有三种迭代方式:
Set<Object> keySet = hashMap.keySet();
Iterator<Object> iterator1 = keySet.iterator();
while (iterator1.hasNext()){
Object next = iterator1.next();
}
Collection<Object> values = hashMap.values();
Iterator<Object> valuesIterator = values.iterator();
while (valuesIterator.hasNext()){
Object next = valuesIterator.next();
}
Set<Map.Entry<Object, Object>> entries = hashMap.entrySet();
Iterator<Map.Entry<Object, Object>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<Object, Object> next = iterator.next();
}
现解读keySet获取的迭代器相关的源码:


keySet是HashMap的父类AbstractMap的成员变量。AbstractMap也有keySet()方法,根据多态的原则,无疑“hashMap.keySet()”调用的是HashMap自己的keySet()方法。在HashMap的keySet()方法里首次执行该方法时必然ks为null,所以执行ks=new HashMap.KeySet(),即调用HashMap的内部类keySet的构造方法来实例化ks对象:
则可知,通过keySet.iterator()得到的迭代器是HashMap的迭代器内部类KeyIterator对象:

可知KeyIterator只重写了Iterator接口的next()方法,next()方法的返回值是this.nextNode().key,而nextNode()是KeyIterator的父类HashIterator的方法。
KeyIterator继承了HashIterator实现了Iterator接口,KeyIterator和它的父类HashIterator分别重写了Iterator接口的next()方法和hasNext()方法。因为调用EntryIterator、ValueIterator和KeyIterator的hasNext()的目的都是一样的(只是想判断是否还有下一个元素),所以只要在EntryIterator、ValueIterator和KeyIterator的父类HashIterator重写hasNext()方法即可,而无需在此三个类重写该方法。而调用EntryIterator、ValueIterator和KeyIterator的next()方法含义是不一样的,即分别获取HashMap存的下一个元素、HashMap存的下一个元素的value和HashMap存的下一个元素的key,所以next()方法在这三个类进行了重写。
下面详解HashIterator:
abstract class HashIterator {
HashMap.Node<K, V>next;
HashMap.Node<K, V>current;
int expectedModCount;
int index;
HashIterator() {
this.expectedModCount = HashMap.this.modCount;
HashMap.Node<K, V>[] t = HashMap.this.table;
this.current = this.next = null;
this.index = 0;
if (t != null && HashMap.this.size >0) {
**//next是table数组中第一个有值的Node节点。**
while(this.index < t.length && (this.next = t[this.index++]) == null) {
}
}
}
public final boolean hasNext() {
return this.next != null;
}
final HashMap.Node<K, V>nextNode() {
HashMap.Node<K, V> e = this.next;
if (HashMap.this.modCount != this.expectedModCount) {
throw new ConcurrentModificationException();
} else if (e == null) {
throw new NoSuchElementException();
} else {
HashMap.Node[] t;
**//如果下一个不为null则返回,这样多次调用nextNode()可以抵达链表或红黑树的末尾**
if ((this.next = (this.current = e).next) == null && (t = HashMap.this.table) != null) {
**//已经遍历完数组上个元素为根的链表或红黑树,接着往数组下一个位置遍历。直到数组最后一个位置**
while(this.index < t.length && (this.next = t[this.index++]) == null) {
}
}
return e;
}
}