我试图理解HashMap中的EntrySet()函数,但我不确定它是如何工作的,也不确定在创建新的entrySet()时,值是从哪里填充的。
public Set<Map.Entry<K,V>> entrySet() {
return entrySet0();
}
private Set<Map.Entry<K,V>> entrySet0() {
Set<Map.Entry<K,V>> es = entrySet;
return es != null ? es : (entrySet = new EntrySet());
}发布于 2017-07-20 02:12:34
以下是jdk8中entrySet()的源代码:
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()方法,可以在for ...loop中使用。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;
}
}根据新对象的顺序执行
。它将基于以下顺序使用构造函数>:Object -> Superclass ->最后一个将是它自己
jsut like :新的HashIterator()然后是新的EntryIterator()
当新建EntryIterator()时,它将在自身之前使用HashIterator的构造函数方法。我们可以看到,当我们使用HashIterator的构造函数方法时,它将加载HashMap的数据。
nextNode()方法从这些数据中获取数据。所以我们可以用来...循环来获取HashMap对象的所有节点。
发布于 2015-06-08 14:13:04
Source
在HashMap内部有一个内部类
private final class EntrySet extends AbstractSet...这是HashMap中的entrySet()方法返回的内容。
当您调用EntrySet类中的方法以检查其内容时,它将在HashMap中查找信息。如果在EntrySet中添加或删除项目,将影响HashMap (反之亦然)。它本质上只是看待同一容器的另一种方式。它没有自己的Map内容副本。
发布于 2015-06-08 14:26:06
创建新EntrySet时填充值的
()
您在HashMap.java中看到的EntrySet并不是一个新的集合,而是一个由HashMap本身支持的功能性包装器(请阅读javadoc)。
对EntrySet的操作被委托给HashMap本身。
因此,EntrySet实际上并不包含任何内容。不需要填充EntrySet。
在源代码中:
private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public Iterator<Map.Entry<K,V>> iterator() {
return newEntryIterator();
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<K,V> e = (Map.Entry<K,V>) o;
Entry<K,V> candidate = getEntry(e.getKey());
return candidate != null && candidate.equals(e);
}
public boolean remove(Object o) {
return removeMapping(o) != null;
}
public int size() {
return size;
}
public void clear() {
HashMap.this.clear();
}
}正如您所看到的,该类不是一个集合,而是实际Map的一个视图。
https://stackoverflow.com/questions/30702183
复制相似问题