首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >entrySet()在HashMap内部是如何工作的?

entrySet()在HashMap内部是如何工作的?
EN

Stack Overflow用户
提问于 2015-06-08 14:03:38
回答 6查看 4.5K关注 0票数 2

我试图理解HashMap中的EntrySet()函数,但我不确定它是如何工作的,也不确定在创建新的entrySet()时,值是从哪里填充的。

代码语言:javascript
复制
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());
}
EN

回答 6

Stack Overflow用户

发布于 2017-07-20 02:12:34

以下是jdk8中entrySet()的源代码:

代码语言:javascript
复制
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的源代码:

代码语言:javascript
复制
final class EntryIterator extends HashIterator implements Iterator<Map.Entry<K,V>> {
    public final Map.Entry<K,V> next() { return nextNode(); }
}

从代码中,我们可以看到它实现了next()方法。并返回nextNode();

第三:我们阅读了nextNode方法的源代码(它在HashIterator类中):

代码语言:javascript
复制
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对象的所有节点。

票数 5
EN

Stack Overflow用户

发布于 2015-06-08 14:13:04

Source

HashMap内部有一个内部类

代码语言:javascript
复制
private final class EntrySet extends AbstractSet...

这是HashMap中的entrySet()方法返回的内容。

当您调用EntrySet类中的方法以检查其内容时,它将在HashMap中查找信息。如果在EntrySet中添加或删除项目,将影响HashMap (反之亦然)。它本质上只是看待同一容器的另一种方式。它没有自己的Map内容副本。

票数 1
EN

Stack Overflow用户

发布于 2015-06-08 14:26:06

创建新EntrySet时填充值的

()

您在HashMap.java中看到的EntrySet并不是一个新的集合,而是一个由HashMap本身支持的功能性包装器(请阅读javadoc)。

对EntrySet的操作被委托给HashMap本身。

因此,EntrySet实际上并不包含任何内容。不需要填充EntrySet。

在源代码中:

代码语言:javascript
复制
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的一个视图。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30702183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档