Monday, August 19, 2019

LinkedHashMap


public class LinkedHashMap<K,V>  extends HashMap<K,V>
                                  implements Map<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values

All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>

LinkedHashMap:
LinkedHashMap is same like HashMap with additional feature of maintaining an insertion order of elements inserted into it.LinkedHashMap contains unique elements. This class provides all of the optional Map operations, and permits null elements. Like HashMap.

How does LinkedHashMap maintain insertion order?
LinkedHashMap implements Map interface  extends HashMap class. its is base on Hash table and linked list implementation of the Map interface. It maintains a doubly-linked list all of its entries, in the map to maintain insertion order, in which they were inserted. This allows insertion-order iteration over the map. That is,when iterating through a collection-view of a LinkedHashMap, the elements will be returned in the order in which they were inserted.

Note: In LinkedHashMap, hashCode() is used to calculate the bucket and therefore calculate the index.

Note:- LinkedHashMap is good data structure to implement Least Recently Used (LRU) cache.

No comments:

Post a Comment