2 u013933280 u013933280 于 2016.01.22 23:00 提问

关于HashMap的KeySet方法的源码问题

初学java,想看看keySet方法到底返回个什么东西,就找源码看了看,其他的都能看懂,就是不明白这个方法第一行的keySet是从哪里来的,注意是小写的k,整个HashMap源文件内我都没找到有keySet这个东西的定义,它怎么能给ks赋值呢,想不明白了。

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

    private final class KeySet extends AbstractSet<K> {
        public Iterator<K> iterator() {
            return newKeyIterator();
        }
        public int size() {
            return size;
        }
        public boolean contains(Object o) {
            return containsKey(o);
        }
        public boolean remove(Object o) {
            return HashMap.this.removeEntryForKey(o) != null;
        }
        public void clear() {
            HashMap.this.clear();
        }
    }

4个回答

caozhy
caozhy   Ds   Rxr 2016.01.22 23:12

keySet应该是一个成员变量,如果不在当前类定义中,就在外覆类、基类中。

Mr_dsw
Mr_dsw   Ds   Rxr 2016.01.23 12:19

他应该是个成员变量,你搜索下,在代码中应该能找到

rui888
rui888   Ds   Rxr 2016.01.23 15:16

/**
* Each of these fields are initialized to contain an instance of the
* appropriate view the first time this view is requested. The views are
* stateless, so there's no reason to create more than one of each.
*/
transient volatile Set keySet;
transient volatile Collection values;

    /**
 * {@inheritDoc}
 *
 * @implSpec
 * This implementation returns a set that subclasses {@link AbstractSet}.
 * The subclass's iterator method returns a "wrapper object" over this
 * map's <tt>entrySet()</tt> iterator.  The <tt>size</tt> method
 * delegates to this map's <tt>size</tt> method and the
 * <tt>contains</tt> method delegates to this map's
 * <tt>containsKey</tt> method.
 *
 * <p>The set is created the first time this method is called,
 * and returned in response to all subsequent calls.  No synchronization
 * is performed, so there is a slight chance that multiple calls to this
 * method will not all return the same set.
 */
public Set<K> keySet() {
    if (keySet == null) {
        keySet = new AbstractSet<K>() {
            public Iterator<K> iterator() {
                return new Iterator<K>() {
                    private Iterator<Entry<K,V>> i = entrySet().iterator();

                    public boolean hasNext() {
                        return i.hasNext();
                    }

                    public K next() {
                        return i.next().getKey();
                    }

                    public void remove() {
                        i.remove();
                    }
                };
            }

            public int size() {
                return AbstractMap.this.size();
            }

            public boolean isEmpty() {
                return AbstractMap.this.isEmpty();
            }

            public void clear() {
                AbstractMap.this.clear();
            }

            public boolean contains(Object k) {
                return AbstractMap.this.containsKey(k);
            }
        };
    }
    return keySet;
}
a8761087
a8761087   Rxr 2016.01.23 19:29

按ctrl+F搜索,如果搜索不到,一定在父类

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!