How linkedhashmap maintains insertion order

15,484

http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html.

Idea behind implementation is quite simple. It extends regular hashMap (so it has all hashMap goodies) but also builds double linked list when adding elements.

(entries are also extended from the HashMap.Entry so they have pointers to after and before) So all entries are ordered HEAD -> Entry1 <-> Entry2 ... <-- TAIL

and at the same time kept in standard HashSet (i assume you are familiar with implementation).

Now when iterating It Linked list of entries is used.

Share:
15,484
Newbie
Author by

Newbie

Updated on July 25, 2022

Comments

  • Newbie
    Newbie almost 2 years

    I know how Hashmap works internally. Linkedhashmap is extending Hashmap class. So how Linkedhashmap is able to maintain the insertion order. I have read the javadoc for Linkedhashmap, but it does not have any details about this. Can somebody help me understand this?

    Thanks in advance.