Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding acessOrder LinkedHashMap Implementation in java

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

like image 352
nanosoft Avatar asked Jul 29 '15 11:07

nanosoft


People also ask

How is LinkedHashMap implemented in Java?

The LinkedHashMap class is very similar to HashMap in most aspects. However, the linked hash map is based on both hash table and linked list to enhance the functionality of hash map. It maintains a doubly-linked list running through all its entries in addition to an underlying array of default size 16.

How does LinkedHashMap maintain insertion order in Java?

This class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted.

What is a LinkedHashMap in Java?

The LinkedHashMap class of the Java collections framework provides the hash table and linked list implementation of the Map interface. The LinkedHashMap interface extends the HashMap class to store its entries in a hash table. It internally maintains a doubly-linked list among all of its entries to order its entries.

What is difference between HashMap and LinkedHashMap?

The key difference between HashMap and LinkedHashMap is order. Elements of a HashMap are not in order, totally random, whereas elements of LinkedHashMap are ordered. The entries of a LinkedHashMap are in key insertion order, which is the order in which the keys are inserted in the Map.


1 Answers

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.

like image 90
SDekov Avatar answered Oct 01 '22 15:10

SDekov