I read about LinkedHashMap and from the description (although very interesting) I couldn't understand how it actually does its job under the hood. As a side note, I know how a HashMap works underneath in Java.
So I review the source and still can not figure out how it works. Perhaps I am not grasping something fundamental in OOP in this case so bear with me.
To summarize the part that is confusing to me is the following:
The LinkedHashMap delegates all the calls to its parent HashMap.
Internally it overrides the HashMap.Entry to implement the various recordAccess and recordRemoval methods which seem to implement the logic of the LinkedHashMap
But the actual Entries are inside the table of the base class i.e. the HashMap which instantiates a table of HashMap.Entry and not of LinkedHashMap.Entry.
So I can't figure out how the various recordAccess and recordRemove etc are actually being called.
So can anyone help me understand what's going on here?
Am I correct to think that somehow the LinkedHashedMap.Entry is the type of table created by the HashMap? But how?
UPDATE:
My question is how do the recordAccess are being called. My experiment on this using a derived version of HashMap failed for the reason of Shengyuan Lu (+1) - My bad there
UPDATE:
The following which I tried is the same (I think) as what the LinkedHashMap is doing:
package delete; public class Base<T> { Entry<T>[] table; int idx = 0; @SuppressWarnings("unchecked") public Base(){ System.out.println("In base"); table = new Entry[10]; } public void add(T x){ table[idx] = new Entry(x); table[idx].doSomething(); } static class Entry<T>{ T value; Entry(T x){ this.value = x; System.out.println("Entry::Base"); } void doSomething(){ System.out.println("In Entry base, doing something"); } } } public class Derived<T> extends Base<T> { static class Entry<T> extends Base.Entry<T>{ Entry(T x) { super(x); System.out.println("In Entry derived"); } int val; @Override void doSomething() { System.out.println("In Entry derived doing something really smart!"); } } /** * @param args */ public static void main(String[] args) { Base<String> b = new Derived<String>(); b.add("Test string"); } } But it prints:
In base Entry::Base In Entry base, doing something So the derived Entry is never called.
Is my example different somehow? I can't understand how this works for LinkedHashMap