While having a look inside HashMap class, I came across this method :-
/**
* This method is invoked whenever the value in an entry is
* overwritten by an invocation of put(k,v) for a key k that's already
* in the HashMap.
*/
void recordAccess(HashMap<K,V> m) {
}
Actually, this method is defined inside the inner class of Entry<K, V>
I'm not able to make out of that comment. What does this method do ?
PS : I can also see this method being called inside's HashMap's putForNullKey() method
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this); // call
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
UPDATE : I've updated the first code snippet.
A LinkedHashMap can have two orders: insertion order, or access order. If access order is used, this method makes sure that the accessed entry is moved to the beginning of the list.