7

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 ?

enter image description here

nanosoft
  • 2,913
  • 4
  • 41
  • 61

1 Answers1

4

You've done some good digging into the code which is great! However LinkedHashMap is not as complex as you 'want' it to be. It is actually a very simple extension of HashMap with the solely purpose of preserving the order of which the elements were added†.

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

This is correct, LinkedHashMap.Entry extends HashMap.Entry but adds the linked bit by storing pointers to before and after entries.

HashMap

enter image description here

LinkedHashMap

enter image description here

Even though the LinkedHashMap diagram looks a lot more complex, it is simply adding the before and after pointers represented by the Blue/Red arrows.

LinkedHashMap also supports access-order (ordering mode) apart from insertion-order as a constructor argument. If flag is set to true iterating over the list will return the elements in the order they were accessed rather than the order in which they were inserted.

Images from JavaArticles.

SDekov
  • 9,276
  • 1
  • 20
  • 50
  • Thanks for JavaArticles link. It testifies(though at high level) what I made out of code inspection. And yes I don't want the data structure to be complex. I found it complex at first go :) and I believe I am not alone into this. – nanosoft Jul 31 '15 at 11:58