How is the internal implementation of LinkedHashMap different from HashMap implementation?
HashMap does not maintain insertion order, hence it does not maintain any doubly linked list.
Most salient feature of LinkedHashMap is that it maintains insertion order of key-value pairs. LinkedHashMap uses doubly Linked List for doing so.
Entry of LinkedHashMap looks like this-
static class Entry<K, V> {
K key;
V value;
Entry<K,V> next;
Entry<K,V> before, after; //For maintaining insertion order
public Entry(K key, V value, Entry<K,V> next){
this.key = key;
this.value = value;
this.next = next;
}
}
By using before and after - we keep track of newly added entry in LinkedHashMap, which helps us in maintaining insertion order.
Before refers to previous entry and after refers to next entry in LinkedHashMap.
For diagrams and step by step explanation please refer http://www.javamadesoeasy.com/2015/02/linkedhashmap-custom-implementation.html
Thanks..!!
So, it has an array of
Entry
objects.
Not exactly. It has an array of Entry
object chains. A HashMap.Entry
object has a next
field allowing the Entry
objects to be chained as a linked list.
I was wondering how can an index of this array store multiple
Entry
objects in case of same hashCode but different objects.
Because (as the picture in your question shows) the Entry
objects are chained.
How is this different from
LinkedHashMap
implementation? Its doubly linked list implementation of map but does it maintain an array like the above and how does it store pointers to the next and previous element?
In the LinkedHashMap
implementation, the LinkedHashMap.Entry
class extends the HashMap.Entry
class, by adding before
and after
fields. These fields are used to assemble the LinkedHashMap.Entry
objects into an independent doubly-linked list that records the insertion order. So, in the LinkedHashMap
class, the entry objects are in two distinct chains:
a singly linked hash chain that is accessed via the main hash array, and
a separate doubly linked list of all entries that is kept in entry insertion order.
Take a look for yourself. For future reference, you can just google:
java LinkedHashMap source
HashMap
uses a LinkedList
to handle collissions, but the difference between HashMap
and LinkedHashMap
is that LinkedHashMap
has a predicable iteration order, which is achieved through an additional doubly-linked list, which usually maintains the insertion order of the keys. The exception is when a key is reinserted, in which case it goes back to the original position in the list.
For reference, iterating through a LinkedHashMap
is more efficient than iterating through a HashMap
, but LinkedHashMap
is less memory efficient.
In case it wasn't clear from my above explanation, the hashing process is the same, so you get the benefits of a normal hash, but you also get the iteration benefits as stated above, since you're using a doubly linked list to maintain the ordering of your Entry
objects, which is independent of the linked-list used during hashing for collisions, in case that was ambiguous..
EDIT: (in response to OP's comment):
A HashMap
is backed by an array, in which some slots contain chains of Entry
objects to handle the collisions. To iterate through all of the (key,value) pairs, you would need to go through all of the slots in the array and then go through the LinkedLists
; hence, your overall time would be proportional to the capacity.
When using a LinkedHashMap
, all you need to do is traverse through the doubly-linked list, so the overall time is proportional to the size.