How is entrySet getting initialized in ?

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 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() {

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

    ankur, Feb 22, 2009
    1. Advertisements

  2. [snippet containing inner class EntrySet followed]
    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.

    Michael Rauscher, Feb 22, 2009
    1. Advertisements

  3. ankur

    ankur Guest

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

    ankur, Feb 23, 2009
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.