What is LinkedHashMap in Java?
The LinkedHashMap class of the Collections framework is the Hashtable and LinkedList implementation of the Map interface. It stores its entries in a Hashtable and uses a doubly-linked list internally to maintain the insertion order. This LinkedList defines the insertion order which is the order in which keys were inserted into the Map. The HashMap provided the benefit of quick insertion, searching, and deletion but in LinkedHashMap the implementation of LinkedList maintains the insertion order as well.Features of LinkedHashMap
- It contains values based on key.
- It contains unique elements.
- It may have one null key and multiple null values.
- It is non-synchronized.
- It maintains the insertion order which causes the iteration to be more expensive.
Performance Factors
The performance of the LinkedHashMap depends on mainly two factors which are described below.Capacity
The capacity means the number of entries it can store. The default capacity is 16 if no parameter is provided. Load factor The load factor means whenever the HashMap is populated to a specific percentage provided as a parameter, a new HashMap is created which is double in size, and all the entries are moved to this new one. The default load factor for a HashMap is 0.75. It is also known as the fill ratio.Declaration of LinkedHashMap
To create a LinkedHashMap, first, we need to import the package, which is java.util.LinkedHashMap.
LinkedHashMap<Key, Value> lhm = new LinkedHashMap<>(12, 0.5f);
Here,
Key is the type of keys in the map.
Value is the type of mapped values in the map.
12 is the capacity which means it will store 12 entries in the map.
0.5f is the load factor which means when 50% is populated then it will create a new Map and move all the values into this new one.
Common Methods of LinkedHashMap
Here is a list of some common methods of LinkedHashMap.Methods | Description |
---|---|
clear() | Removes all the mappings in the map. |
containsValue(Object value) | Returns true if the map maps the key to the specified value. |
entrySet() | Returns the Set view of the mappings in the map. |
get(Object key) | Returns the value against the specified key or null if no key is specified. |
keySet() | Returns the Set view of the keys in the map. |
values() | Returns the Collection view of the values in the map. |
getOrDefault(Object key, V defaultValue) | Returns the value to which the specified key is associated or defaultValue if no key is associated. |
LinkedHasMap Example
import java.util.LinkedHashMap;
class Main {
public static void main(String[] args) {
// Creating a LinkedHashMap of odd numbers
LinkedHashMap<String, Integer> oddNumbers = new LinkedHashMap<>();
// Using the put() method
oddNumbers.put("One", 1);
oddNumbers.put("Three", 3);
System.out.println("LinkedHashMap1: " + oddNumbers);
// Creating another LinkedHashMap
LinkedHashMap<String, Integer> numbers = new LinkedHashMap<>();
numbers.put("Two", 2);
// Using putAll() method
numbers.putAll(oddNumbers);
System.out.println("LinkedHashMap2: " + numbers);
// Using entrySet() method
System.out.println("Key/Value mappings: " + numbers.entrySet());
// Using keySet() method
System.out.println("Keys: " + numbers.keySet());
// Using values() method
System.out.println("Values: " + numbers.values());
// Using get() method
int value1 = numbers.get("Three");
System.out.println("Returned Number: " + value1);
// Using getOrDefault() method
int value2 = numbers.getOrDefault("Five", 5);
System.out.println("Returned Number: " + value2);
// Using remove() method
int value = numbers.remove("Two");
System.out.println("Removed value: " + value);
// Using entrySet() method
System.out.println("Key/Value mappings: " + numbers.entrySet());
}
}
Output
LinkedHashMap1: {One=1, Three=3}
LinkedHashMap2: {Two=2, One=1, Three=3}
Key/Value mappings: [Two=2, One=1, Three=3]
Keys: [Two, One, Three]
Values: [2, 1, 3]
Returned Number: 3
Returned Number: 5
Removed value: 2
Key/Value mappings: [One=1, Three=3]
LinkedHashMap vs HashMap
Let’s see some differences between them, whereas both implement the Map interface.- LinkedHashMap maintains the insertion order as it implements the doubly-linked list internally.
- LinkedHashMap needs more storage as it implements the LinkedList.
- Regarding performance, the LinkedHashMap is slower than HashMap.
GO TO FULL VERSION