以下是的entrySet()的jdk8的源代码:
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();//get the iterator
}
public final boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Node<K,V> candidate = getNode(hash(key), key);
return candidate != null && candidate.equals(e);
}
public final boolean remove(Object o) {
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Object value = e.getValue();
return removeNode(hash(key), key, value, true, true) != null;
}
return false;
}
public final Spliterator<Map.Entry<K,V>> spliterator() {
return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0);
}
public final void forEach(Consumer<? super Map.Entry<K,V>> action) {
Node<K,V>[] tab;
if (action == null)
throw new NullPointerException();
if (size > 0 && (tab = table) != null) {
int mc = modCount;
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next)
action.accept(e);
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}
}
首先:当我们使用entrySet()方法时,它返回新的EntrySet(),它是EntrySet的一个实例。这个类有一个iterator()方法,可以用于...循环。而iterator()方法返回一个迭代器(类:EntryIterator)
第二:我们读到最后一类EntryIterator的源代码:
final class EntryIterator extends HashIterator implements Iterator<Map.Entry<K,V>> {
public final Map.Entry<K,V> next() { return nextNode(); }
}
从代码中,我们可以看到它实现了next()方法。它返回nextNode();
第三:我们读nextNode方法的源代码(它是在HashIterator类):
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() { //when new EntryIterator, this will load data first.
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;
}
}
根据新对象的顺序。它将使用构造函数>基于该顺序:对象--->父类--->最后将自己
仅仅指刚,如:新HashIterator(),则新EntryIterator()
当新EntryIterator (),它将在其自身之前使用HashIterator的构造函数 。我们可以看到当我们使用HashIterator的构造函数方法时,它会加载HashMap的数据,当我们使用 时。
而nextNode()方法从这些数据中获取数据。所以我们可以使用for ...循环来获取HashMap Object的所有节点。
如果您正在查看'HashMap'代码,请在'HashMap'中查找内部类'EntrySet'的定义。它实际上是地图内容的实时视图。 – khelwood
感谢@khelwood :) 你指的是这个 - 'code' //浏览 私人转移设定> =的entrySet空; 'code' 但..Can你解释这:) –
user3759177
号,上面写着'私人final类EntrySet' – khelwood