LinkedHashMap in Java

Posted on Updated on

LinkedHashMap extends HashMap class and implements Map interface, so it inherit features of HashMap along with some other features which distinguish it from HashMap. A LinkedHashMap is a combination of hash table and linked list. It maintains a doubly-linked list running through all of its entries to maintain insertion order.

Followings are the features of LinkedHashMap.

  1. It stores key-value pairs.
  2. It contains unique key.
  3. Similar to HashMap, it allows one null key and multiple null values.
  4. Insertion order is maintained.
  5. Performance is likely to be just slightly below that of HashMap, due to the added expense of maintaining the linked list with one exception, Iteration over the LinkedHashMap requires time proportional to the size of the map, regardless of its capacity. While, Iteration over a HashMap is likely to be more expensive, requiring time proportional to its capacity.
  6. It is not synchronized.

LinkedHashMap provides following constructors.

Constructor Description
LinkedHashMap() Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).
LinkedHashMap(int initialCapacity) Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and a default load factor (0.75). Throws IllegalArgumentException if the initial capacity is negative.
LinkedHashMap(Map map) Constructs an insertion-ordered LinkedHashMap instance with the same mappings as the specified map.
LinkedHashMap(int initialCapacity, float loadFactor) Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and load factor. Throws IllegalArgumentException if the initial capacity is negative or the load factor is nonpositive.
LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode. Throws IllegalArgumentException if the initial capacity is negative or the load factor is nonpositive. accessOrder: the ordering mode – truefor access-order, false for insertion-order.

Example of LinkedHashMap.

Output

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.