Last Updated: 28 May, 2022
The LinkedHashMap class is a child class of HashMap. It was introduced in the Java 1.4 version and is available in the java.util package.
A LinkedHashMap is used to store the data in the form of key-value pairs. The implementation of LinkedHashMap is based on hash table and linked list.
LinkedHashMap maintains the insertion order of data.
As we can see from the complete hierarchy above, LinkedHashMap extends the HashMap class and implements the Map interface.
Here, K is the key Object type and V is the value Object type.
Output
Iterating LinkedHashMap and printing Key and Value
in - India
us - United State
en - England
ca - Canada
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). |
LinkedHashMap(int initialCapacity, float loadFactor) | Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and load factor. |
LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) | Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode. |
LinkedHashMap(Map<? extends K,? extends V> m) | Constructs an insertion-ordered LinkedHashMap instance with the same mappings as the specified map. |
The LinkedHashMap class in java has the following methods.
Methods with Description |
---|
public boolean containsValue(Object value) Returns true if this map maps one or more keys to the specified value. |
public V get(Object key) Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. |
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) Returns true if this map should remove its eldest entry. |
public Set<K> keySet() Returns a Set view of the keys contained in this map. |
public Collection<V> values() Returns a Collection view of the values contained in this map. |
public Set<Map.Entry<K,V>> entrySet() Returns a Set view of the mappings contained in this map. |
The Java LinkedHashMap class provided methods are implemented in the below given examples. Let's go through the examples one by one and understand the uses for each method.
Example 1 : Methods for Adding elements in LinkedHashMap
Output
LinkedHashMap Elements:{101=C, 102=C++, 103=Java, 104=Python}
After calling putIfAbsent():{101=C, 102=C++, 103=Java, 104=Python, 105=Go Lang}
LinkedHashMap Elements after adding another LinkedHashMap:
{101=C, 102=C++, 103=Java, 104=Python, 105=Go Lang, 111=MySQL, 112=MongoDB}
Example 2 : Methods for Accessing elements from LinkedHashMap
Output
LinkedHashMap Elements:{101=C, 102=C++, 103=Java, 104=Python, 105=Go Lang}
Iterate the map using for-each loop
Key: 101 Value: C
Key: 102 Value: C++
Key: 103 Value: Java
Key: 104 Value: Python
Key: 105 Value: Go Lang
After called map.get(102): C++
Returns a Collection view of the values contained in this map C
C++
Java
Python
Go Lang
Example 3 : Methods for Updating elements in LinkedHashMap
Output
HashMap Elements:{101=C, 102=C++, 103=Java, 104=Python, 105=Go Lang}
After called map.replace(105, "MySQL"):
{101=C, 102=C++, 103=Java, 104=Python, 105=MySQL}
After called map.replace(103, "Java", "JAVA/J2EE"):
{101=C, 102=C++, 103=JAVA/J2EE, 104=Python, 105=MySQL}
After called map.replaceAll((k,v) -> "MongoDB"):
{101=MongoDB, 102=MongoDB, 103=MongoDB, 104=MongoDB, 105=MongoDB}
Example 4 : Methods for Removing elements from LinkedHashMap
Output
LinkedHashMap Elements:{101=C, 102=C++, 103=Java, 104=Python, 105=Go Lang}
After called map.remove(102):{101=C, 103=Java, 104=Python, 105=Go Lang}
Afte called map.remove(104, "Python"):
{101=C, 103=Java, 105=Go Lang}
After called map.remove(101, "C++"):
{101=C, 103=Java, 105=Go Lang}
After called map.clear():{}
Example 5 : Utility methods of LinkedHashMap
Output
LinkedHashMap Elements:{101=C, 102=C++, 103=Java, 104=Python}
LinkedHashMap Size: 4
Is Map is Empty: false
map.containsKey(105): false
map.containsValue("Java"): true
After calling map.clear():{}
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/LinkedHashMap.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between HashMap and LinkedHashMap?
HashMap | LinkedHashMap |
---|---|
HashMap extends the AbstractMap class. | LinkedHashMap extends the HashMap class. |
HashMap does not maintain the insertion order of elements. | The insertion order of elements is preserved by LinkedHashMap. (Major Difference) |
HashMap uses hash tables to store maps. | LinkedHashMap uses a hash table along with a linked list to store maps. |
HashMap was introduced in the JDK 1.2 version. | LinkedHashMap was introduced in the JDK 1.4 version. |
HashMap required less memory than LinkedHashMap. | LinkedHashMap required more memory than LinkedHashMap. |
HashMap gives faster performance than LinkedHashMap. | LinkedHashMap's performance is a bit slower than HashMap's. |