循环遍历一个 HashMap 能够经过下面的方式java
for (Map.Entry entry : hashMap.entrySet()) { Object key = entry.getKey(); Object value = entry.getValue(); .... }
或者用迭代器数组
Iterator iterator = hashMap.entrySet().iterator(); while (iterator.hasNext()) { Object object = iterator.next(); .... }
实际上是同样的 foreach 底层就是调用了 iterator.next() 方法。源码分析
下面经过源码分析一下究竟如何遍历一个map和遍历一个map具体发生了哪些事情。this
进入到这个 entrySet() 方法里面code
public Set<Map.Entry<K,V>> entrySet() { Set<Map.Entry<K,V>> es; return (es = entrySet) == null ? (entrySet = new EntrySet()) : es; } final class EntrySet extends AbstractSet<Map.Entry<K,V>> { public final int size() { return size; } public final void clear() { HashMap.this.clear(); } public final Iterator<Map.Entry<K,V>> iterator() { return new EntryIterator(); } ... }
若是是空的就new一个EntrtSet(),若是不是空的就直接返回,在看下EntrySet这个类,里面有一个 iterator() 方法,new一个EntryIterator()继承
final class EntryIterator extends HashIterator implements Iterator<Map.Entry<K,V>> { public final Map.Entry<K,V> next() { return nextNode(); } } 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 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; } public final void remove() { Node<K,V> p = current; if (p == null) throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); current = null; K key = p.key; removeNode(hash(key), key, null, false, false); expectedModCount = modCount; } }
这个 EntryIterator 继承自 HashIterator 实现了 Iterator 接口,这里的 next() 方法直接调用的是HashIterator类的 nextNode() 方法,从这能够看出来这个next方法其实返回的就是 Node 节点的 next 字段。Node节点是这样的接口
static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; V value; Node<K,V> next; 。。。 }
综上所述,用entrySet这种方式遍历一个map,其实就是获取到hashmap最底层的数组,从数组的第0个位置一直开始遍历,若是数组的位置是个链表,那么把链表也遍历一遍。回过头来看这个方法的名字 entrySet,hashmap的数组里面放的是Node,这个Node就是Map.Entry的实现类,entrySet 顾名思义就是全部 entry 的一个set集合。rem