Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the internal implementation of LinkedHashMap different from HashMap implementation?

I read that HashMap has the following implementation:

main array     ↓ [Entry] → Entry → Entry      ← linked-list implementation [Entry] [Entry] → Entry [Entry] [null ] 

So, it has an array of Entry objects.

Questions:

  1. I was wondering how can an index of this array store multiple Entry objects in case of same hashCode but different objects.

  2. 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?

like image 639
Mercenary Avatar asked Nov 02 '13 04:11

Mercenary


People also ask

What is the main 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.

What is internal implementation of LinkedHashMap in Java?

LinkedHashMap is the data structure used to store the key-value pairs like HashMap but it guarantees the order of insertion(unlike the HashMap). So the elements are stored in the order of their insertion.

How HashMap is implemented internally?

How LinkedHashMap Work Internally? Hash: All the input keys are converted into a hash which is a shorter form of the key so that the search and insertion are faster. Key: Since this class extends HashMap, the data is stored in the form of a key-value pair. Therefore, this parameter is the key to the data.

What is difference between HashMap and LinkedList?

HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. LinkedList is a part of the Collection framework present in java. util package.


1 Answers

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.

LinkedHashMap

For diagrams and step by step explanation please refer http://www.javamadesoeasy.com/2015/02/linkedhashmap-custom-implementation.html

Thanks..!!

like image 87
Ankit Mittal Avatar answered Sep 20 '22 10:09

Ankit Mittal