简体   繁体   中英

jre1.6.0_27 HashMap values() sourcecode

Today, I opened jre1.6.0_27 HashMap values() method sourcecode

  389  public Set<K> keySet()
  390  {
  391      Set localSet = this.keySet;
  392      return (this.keySet = new KeySet(null));
  393  }
  394
  395  public Collection<V> values()
  396  {
  397      Collection localCollection = this.values;
  398      return (this.values = new Values(null));
  399  }

I think these sourcecode are error, but I don't know why they look like this. Who can tell me why?

=======================================

Thanks everyone, I think this is Eclipse problem, this sourcecode I used Eclipse F3 and went to it, so it looks like the above that.

I just open src.zip, this method sourcecode is right.

/**
 * Returns a {@link Collection} view of the values contained in this map.
 * The collection is backed by the map, so changes to the map are
 * reflected in the collection, and vice-versa.  If the map is
 * modified while an iteration over the collection is in progress
 * (except through the iterator's own <tt>remove</tt> operation),
 * the results of the iteration are undefined.  The collection
 * supports element removal, which removes the corresponding
 * mapping from the map, via the <tt>Iterator.remove</tt>,
 * <tt>Collection.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.
 */
903    public Collection<V> values() {
904        Collection<V> vs = values;
905        return (vs != null ? vs : (values = new Values()));
906    }

At least in OpenJDK 7 it looks correct:

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

  916       public Collection<V> values() {
  917           Collection<V> vs = values;
  918           return (vs != null ? vs : (values = new Values()));
  919       }

Why do you think these methods are wrong? You have to look at the source code of the inner classes KeySet and Values inside HashMap to understand how these work.

The keySet() method returns a new KeySet object. In JDK 1.6.0_35, the source code of the inner class KeySet looks like this:

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

It's an implementation of Set that gets its data from the HashMap .

Likewise there's an inner class Values that works in the same way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM