Java LinkedHashMap Class

Filed Under: Java
LinkedHashMap Java

LinkedHashMap in Java

  1. It’s part of Java Collections Framework.
  2. LinkedHashMap is the hash table and linked list implementation of the Map interface.
  3. LinkedHashMap extends HashMap class.
  4. LinkedHashMap maintains the order of insertion. So while iterating over its keys, the elements are returned in the order they were inserted.
  5. LinkedHashMap uses a doubly-linked list to maintain the order of insertion.
  6. If a key is reinserted, its insertion order is not affected.
  7. It’s useful when you want a Map where we can iterate over the entries in the order of insertion but don’t want to deal with the additional overhead associated with the TreeMap.
  8. LinkedHashMap allows null entries for key and value. There can be a single null key and multiple null values.
  9. LinkedHashMap is not thread-safe. If you want to use it in a multi-threaded environment, you can create a synchronized wrapper over it using the Collections.synchronizedMap() method.

LinkedHashMap Constructors

There are 5 constructors in LinkedHashMap class.

LinkedHashMap Constructors

LinkedHashMap Constructors

  1. LinkedHashMap(): creates an empty LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).
  2. LinkedHashMap(int initialCapacity): creates an empty LinkedHashMap instance with the given initial capacity and load factor (0.75). It throws IllegalArgumentException if the initialCapacity is negative.
  3. LinkedHashMap(int initialCapacity, float loadFactor): creates an empty LinkedHashMap instance with the given initial capacity and load factor. It throws IllegalArgumentException if the initial capacity is negative or the load factor is nonpositive.
  4. LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder): creates an empty LinkedHashMap with the given initial capacity, load factor, and ordering mode. If accessOrder is true – the ordering mode is access-order, otherwise insertion-order.
  5. LinkedHashMap(Map<? extends K, ? extends V> m): creates an insertion-ordered LinkedHashMap instance with the same mappings as the specified map. It throws NullPointerException if the given map is null.

Here is a simple example to confirm that the LinkedHashMap maintains the order of insertion for its keys.


package com.journaldev.examples;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class LinkedHashMapExamples {

	public static void main(String[] args) {

		Map<Integer, String> vowelsMap = new HashMap<>();
		vowelsMap.put(1, "a");
		vowelsMap.put(2, "e");
		vowelsMap.put(5, "u");
		vowelsMap.put(4, "o");
		vowelsMap.put(3, "i");

		vowelsMap.forEach((k, v) -> System.out.println("[" + k + "," + v + "]"));

		LinkedHashMap<Integer, String> lhm = new LinkedHashMap<>();
		lhm.put(1, "A");
		lhm.put(2, "E");
		lhm.put(5, "U");
		lhm.put(4, "O");
		lhm.put(3, "I");

		lhm.forEach((k, v) -> System.out.println("[" + k + "," + v + "]"));
	}
}

Output:


[1,a]
[2,e]
[3,i]
[4,o]
[5,u]

[1,A]
[2,E]
[5,U]
[4,O]
[3,I]

LinkedHashMap Methods

There are no exclusive methods for LinkedHashMap class. It utilizes the methods from the HashMap class by extending it. But, some of the methods are overridden for the insertion order of entries.

  • containsValue(Object value): utilizes the LinkedList approach for faster resolution.
  • get(Object key), getOrDefault(Object key, V defaultValue)
  • clear()
  • keySet(): an instance of LinkedKeySet is returned.
  • forEach(), replaceAll(): utilizes the linked list implementation approach for insertion-order iteration and faster processing.

LinkedHashMap vs HashMap

  • LinkedHashMap extends HashMap class. So HashMap is the superclass of LinkedHashMap class.
  • LinkedHashMap maintains the order of insertion where HashMap doesn’t maintain any ordering of entries.
  • Iterating over the LinkedHashMap entries always produce the same sequence of elements. Iterating over the HashMap entries can produce random sequence because the order is not maintained.
  • LinkedHashMap performance is slightly below that of HashMap, due to the added expense of maintaining the linked list, with one exception: Iteration over the collection-views of a LinkedHashMap requires time proportional to the size of the map, regardless of its capacity. Iteration over a HashMap is likely to be more expensive, requiring time proportional to its capacity.

LinkedHashMap vs TreeMap

If you want to access Map elements in the insertion-order, LinkedHashMap is recommended. It comes with fast performance and without incurring the increased cost associated with TreeMap.

Reference: LinkedHashMap API Docs

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages