6

mac OS 上的 JDK 8,查看 HashMap.java 中的以下代码:

    public Set<K> keySet() {
        Set<K> ks = keySet;
        if (ks == null) {
            ks = new KeySet();
            keySet = ks;
        }
        return ks;
    }

对返回的 ks 的任何更改都将反映在 keySet 中,因为它们始终指向相同的底层集合,如果这是真的,可以写成:

    public Set<K> keySet() {
        if (keySet == null) {
            keySet = new KeySet();
        }
        return keySet;
    }

这两个代码片段的行为是否相同?

如果是这样,为什么HashMap使用第一个变体而不是第二个变体?

4

2 回答 2

3

缓存到局部变量是为了提高性能。生成的字节码更小,该字段被读取一次,因此缓存未命中可能只发生一次,以及其他一些事情。

这是一个相当高级的优化,应该只在非常频繁运行的代码片段上进行。之所以在这里应用它,很可能是因为它HashMap是用 Java 1.2 编写的,当时 JIT 非常基础,因此这些东西产生了相当大的影响。

在这种情况下,也是为了支持多线程访问。 HashMap不同步,但是如果以后不修改它可以通过安全发布共享。如果两个线程同时执行该方法,可能会出现竞争条件:第一个读入if(keySet == null)可以读取另一个线程写入的较新值,而第二个读入return keySet;读取较旧的 ( null) 值。使用局部变量可确保ifreturn非空时使用相同的引用。所以它永远无法返回null

于 2020-11-20T06:14:00.063 回答
-1

正如@Fransesco 指出的那样,局部变量仅作为优化保存。它还避免了在少数情况下创建新对象。

该实现在内部不存储任何状态,并且对所有操作的底层哈希图进行操作,并且根据 java 文档,预期对集合的更改

  • 不允许 add 和 addAll(抛出 UnsupportedOperationException)
  • remove、retainAll 预计会修改底层映射

以供参考

   /**
     * Returns a {@link Set} view of the keys contained in this map.
     * The set is backed by the map, so changes to the map are
     * reflected in the set, and vice-versa.  If the map is modified
     * while an iteration over the set is in progress (except through
     * the iterator's own <tt>remove</tt> operation), the results of
     * the iteration are undefined.  The set supports element removal,
     * which removes the corresponding mapping from the map, via the
     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
     * operations.
     *
     * @return a set view of the keys contained in this map
     */
    public Set<K> keySet() {
        Set<K> ks = keySet;
        if (ks == null) {
            ks = new KeySet();
            keySet = ks;
        }
        return ks;
    }

    final class KeySet extends AbstractSet<K> {
        public final int size()                 { return size; }
        public final void clear()               { HashMap.this.clear(); }
        public final Iterator<K> iterator()     { return new KeyIterator(); }
        public final boolean contains(Object o) { return containsKey(o); }
        public final boolean remove(Object key) {
            return removeNode(hash(key), key, null, false, true) != null;
        }
        public final Spliterator<K> spliterator() {
            return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
        }
        public final void forEach(Consumer<? super K> 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.key);
                }
                if (modCount != mc)
                    throw new ConcurrentModificationException();
            }
        }
    }

AFAIK 行为在所有平台上都是相同的,而不仅仅是 Mac

于 2020-11-20T06:27:53.740 回答