LinkedHashMap in Java
- It’s part of Java Collections Framework.
- LinkedHashMap is the hash table and linked list implementation of the Map interface.
- LinkedHashMap extends HashMap class.
- LinkedHashMap maintains the order of insertion. So while iterating over its keys, the elements are returned in the order they were inserted.
- LinkedHashMap uses a doubly-linked list to maintain the order of insertion.
- If a key is reinserted, its insertion order is not affected.
- 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.
- LinkedHashMap allows null entries for key and value. There can be a single null key and multiple null values.
- 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.
Linked HashMap Constructors
- LinkedHashMap(): creates an empty LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).
- 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. - 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.
- 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.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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 2 3 4 5 6 7 8 9 10 11 12 |
<span style="color: #008000;"><strong><code> [1,a] [2,e] [3,i] [4,o] [5,u] [1,A] [2,E] [5,U] [4,O] [3,I] </code></strong></span> |
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