开发者社区> 问答> 正文

关于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();
        }
    }

展开
收起
蛮大人123 2016-05-26 18:35:23 2479 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    /**
    * 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;
    }
    2019-07-17 19:16:47
    赞同 展开评论 打赏
问答分类:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载