As I understand LinkedHashMap extends HashMap and LinkedHashMap.Entry extends HashMap.Entry as well.
LinkedHashMap has two main attributes 1) header which is a LinkedHashMap.Entry node. and 2) inherited table which is HashMap.Entry[] array. Now the table in LinkedHashMap is assigned array of LinkedHashMap.Entry at runtime and this is taken care by below method:
/**
* This override differs from addEntry in that it doesn't resize the
* table or remove the eldest entry.
*/
void createEntry(int hash, K key, V value, int bucketIndex) {
HashMap.Entry<K,V> old = table[bucketIndex];
Entry<K,V> e = new Entry<>(hash, key, value, old);
table[bucketIndex] = e;
e.addBefore(header);
size++;
}
First three lines of the method actually converts HashMap.Entry to LinkedHashMap.Entry and also maintains the references of after and before of the Entry in a way such that before of header is pointing to last element in order and last element's after is pointing to header forming circle.
Now when we create an instance of either of Maps(using new constructor), no memory is allocated for table attribute as its just initialized to empty table.
Map<String, String> linkedHashMap = new LinkedHashMap<String, String>();
Now lets say we do our puts:-
linkedHashMap.put("a", "A");
linkedHashMap.put("b", "B");
linkedHashMap.put("c", "C");
After these statements our LinkedHashMap data structure(table array is initialized after first put) would look something like in the pic.I marked the elements a,b, and c for easy reference. I understand that the real order would not be the same. I find this data structure quite complex- so many references. It has double linked list maintained quite differently and for different purpose and also single linked list for normal hashmap and that too both in same Entry. Is my understanding correct ?