How is entrySet getting initialized in HashMap.java ?

Discussion in 'Java' started by ankur, Feb 22, 2009.

  1. ankur

    ankur Guest

    Sorry for this irrelevant question , but if I cannot resist asking:

    In the class HashMap.java how is the

    private transient Set<Map.Entry<K,V>> entrySet = null;

    gettting initialized with all the map entries ?

    I could not figure this out by looking at this segment of code that
    also contains the constructor:


    public Set<Map.Entry<K,V>> entrySet() {
    return entrySet0();
    }

    private Set<Map.Entry<K,V>> entrySet0() {
    Set<Map.Entry<K,V>> es = entrySet;
    return es != null ? es : (entrySet = new EntrySet());
    }

    public final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
    public Iterator<Map.Entry<K,V>> iterator() {
    return newEntryIterator();
    }
    public boolean contains(Object o) {
    if (!(o instanceof Map.Entry))
    return false;
    Map.Entry<K,V> e = (Map.Entry<K,V>) o;
    Entry<K,V> candidate = getEntry(e.getKey());
    return candidate != null && candidate.equals(e);
    }
    public boolean remove(Object o) {
    return removeMapping(o) != null;
    }
    public int size() {
    return size;
    }
    public void clear() {
    HashMap1.this.clear();
    }
    }

    How is the statement entrySet = new EntrySet() causing the
    initialization ? Constructor does not seem to do anything about the
    initialization ?

    Thanks,
    Ankur
     
    ankur, Feb 22, 2009
    #1
    1. Advertising

  2. ankur wrote:
    > In the class HashMap.java how is the
    >
    > private transient Set<Map.Entry<K,V>> entrySet = null;
    >
    > gettting initialized with all the map entries ?


    [snippet containing inner class EntrySet followed]

    > How is the statement entrySet = new EntrySet() causing the
    > initialization ? Constructor does not seem to do anything about the
    > initialization ?


    EntrySet doesn't hold any references to entries by itself. It's an inner
    class and therefore has access to members and methods of its enclosing
    class (HashMap).

    E. g. whenever EntrySet#iterator is called, it returns the result of
    HashMap#newEntryIterator(). This method returns a new instance of
    EntryIterator which extends HashIterator. The latter uses the Entry
    array which is managed by HashMap.

    HTH
    Michael
     
    Michael Rauscher, Feb 22, 2009
    #2
    1. Advertising

  3. ankur

    ankur Guest

    On Feb 22, 3:39 pm, Michael Rauscher <> wrote:
    > ankur wrote:
    > > In the class HashMap.java how is the

    >
    > > private transient Set<Map.Entry<K,V>> entrySet = null;

    >
    > > gettting initialized with all the map entries ?

    >
    > [snippet containing inner class EntrySet followed]
    >
    > > How is the statement entrySet = new EntrySet() causing the
    > > initialization ? Constructor does not seem to do anything about the
    > > initialization ?

    >
    > EntrySet doesn't hold any references to entries by itself. It's an inner
    > class and therefore has access to members and methods of its enclosing
    > class (HashMap).
    >
    > E. g. whenever EntrySet#iterator is called, it returns the result of
    > HashMap#newEntryIterator(). This method returns a new instance of
    > EntryIterator which extends HashIterator. The latter uses the Entry
    > array which is managed by HashMap.
    >
    > HTH
    > Michael


    Thanks for your help Michael. I knew I was making a silly mistake
    somewhere.

    Ankur
     
    ankur, Feb 23, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vince Darley
    Replies:
    4
    Views:
    4,432
    emilchacko
    Mar 2, 2010
  2. Steve Green

    HashMap<> entrySet

    Steve Green, Mar 25, 2005, in forum: Java
    Replies:
    2
    Views:
    2,346
    Steve Green
    Mar 25, 2005
  3. Rakesh
    Replies:
    10
    Views:
    12,182
    Mike Schilling
    Apr 8, 2008
  4. Rakesh
    Replies:
    3
    Views:
    4,238
    Tom Anderson
    Mar 10, 2009
  5. laredotornado
    Replies:
    30
    Views:
    3,555
    Mike Schilling
    Apr 26, 2010
Loading...

Share This Page