How is entrySet getting initialized in HashMap.java ?

A

ankur

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
 
M

Michael Rauscher

ankur said:
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
 
A

ankur

ankur said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top