Java – why is it faster to iterate buckets in linked HashMap than HashMap?
It's hard for me to understand that
Google search, I found
and
If this is the case, why does the only HashMap have to iterate over empty buckets instead of LinkedHashMap, although both are implemented using the same bucket concept? All entries are doubly linked, meaning "all buckets and elements are doubly linked", or just "elements are doubly linked"
Please give me a diagram to explain the implementation of bidirectional link bucket in LinkedHashMap
Thank you in advance
Solution
The LinkedHashMap bucket node (entry) holds two additional pointers (before and after) to maintain order
This is the structure at creation time
Map<Integer,String> linkedHashMap = new LinkedHashMap<Integer,String>();
Now let's add an element
linkedHashMap.put(1,"obj1");
This is before the LinkedHashMap header, followed by the entry object
Let's add another element
linkedHashMap.put(15,"obj15");
Check the change of the pointer with the previous state of red
Let's add another element
linkedHashMap.put(4,"obj4");
When another entry inserts the same hash, the next pointer in each entry forms singlelikedlist