当然在父类AbstractMap中啦

解决方案 »

  1.   


    当然在父类AbstractMap中啦父类AbstractMap中也没有相关代码啊,还请明示,最好能粘贴出相关源码给予说明,如果让我明白了,一定给分
      

  2.   

        /**
         * Reset to initial default state.  Called by clone and readObject.
         */
        void reinitialize() {
            table = null;
            entrySet = null;
            keySet = null;
            values = null;
            modCount = 0;
            threshold = 0;
            size = 0;
        }
    这是源码  我贴出来了  keySet是赋了一个默认值的值null的  
    当判断keySet中没有被赋值的时候  就是默认值null 
      * 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;
            return (ks = keySet) == null ? (keySet = new KeySet()) : ks;
        }
      

  3.   

    keySet( )是在父类AbstractMap中实现的
    首先是字段:transient volatile Set<K>        keySet = null;
    然后是方法实现: 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;
        }
      

  4.   


    不明白你的疑问是什么?  就是第一次调用时候new KeySet() 这个对象,  为什么还要赋值? keySet()方法 注释写的很清楚, 这个Set 不直接存储实例,  而是通过内部类的形式直接返回Map中Entry[] t = table 里面的对象.  你看下 KeySet 类的iterator()方法的实现
      

  5.   


    不明白你的疑问是什么?  就是第一次调用时候new KeySet() 这个对象,  为什么还要赋值? keySet()方法 注释写的很清楚, 这个Set 不直接存储实例,  而是通过内部类的形式直接返回Map中Entry[] t = table 里面的对象.  你看下 KeySet 类的iterator()方法的实现
    是我没仔细看注释,你的答复我比较满意,结贴!