I am trying to understand entrySet() function in HashMap but I am not sure how it is working and from where the values are being populated when creating new EntrySet().
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());
}
Source
Inside
HashMap
there is an inner classThis is what is returned by the
entrySet()
method inHashMap
.When you call a method in the
EntrySet
class to examine its contents, it looks up the information in theHashMap
. If you add or remove items in theEntrySet
, it will affect theHashMap
(and vice versa). It is essentially just another way of looking at the same container. It does not have its own copy of theMap
's contents.