Question

According to the HashMap doc on entrySet():

public Set> entrySet ()

Added in API level 1 Returns a set containing all of the mappings in this map. Each mapping is an instance of Map.Entry. As the set is backed by this map, changes in one will be reflected in the other.

Returns a set of the mappings.

But as I inspect the HashMap code I cannot understand how the field entrySet is linked to table or any other object.
I am trying to understand how entrySet is updated when the put() method is invoked.

Was it helpful?

Solution

Let's have a look at the implementation of EntrySet:

965  private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
966 public Iterator<Map.Entry<K,V>> iterator() {
967 return newEntryIterator();
968 }
969 public boolean contains(Object o) {
970 if (!(o instanceof Map.Entry))
971 return false;
972 Map.Entry<K,V> e = (Map.Entry<K,V>) o;
973 Entry<K,V> candidate = getEntry(e.getKey());
974 return candidate != null && candidate.equals(e);
975 }
976 public boolean remove(Object o) {
977 return removeMapping(o) != null;
978 }
979 public int size() {
980 return size;
981 }
982 public void clear() {
983 HashMap.this.clear();
984 }
985 }

Note that it's an inner class and has implicit access to all the state, and all the instance methods, of the enclosing HashMap instance. It implements its methods by calling the appropriate methods on the enclosing HashMap.

OTHER TIPS

EntrySet is a nested class within HashMap see the source code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top