LinkedHashmap in Java

A LinkedHashMap in Java is a specialized implementation of the Map interface that maintains a linked list of the entries in the map, in the order in which they were inserted. In this article, we will take a closer look at how LinkedHashMap works in Java.

What is a LinkedHashMap?

A LinkedHashMap is a subclass of the java.util.HashMap class that provides a way to store key-value pairs in a way that maintains their insertion order. Each key-value pair in a LinkedHashMap is stored as an Entry object that contains a reference to the key, the value, and pointers to the next and previous entries in the linked list.

In addition to the standard Map operations, a LinkedHashMap also provides methods for retrieving the entries in the map in their insertion order, either as a set or as a collection.

How Does LinkedHashMap Work?

When a key-value pair is added to a LinkedHashMap, it is stored as an Entry object that contains a reference to the key, the value, and pointers to the next and previous entries in the linked list. If the key already exists in the map, its value is updated and its position in the linked list is not changed.

When a key is retrieved from a LinkedHashMap, its entry is moved to the end of the linked list, so that the most recently accessed entries are always at the end of the list. This means that the order of the entries in the map reflects the order in which they were last accessed.

LinkedHashMap Example

Here is an example of using a LinkedHashMap to store key-value pairs:

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

public class LinkedHashMapExample {
public static void main(String[] args) {
Map linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("one", "1");
linkedHashMap.put("two", "2");
linkedHashMap.put("three", "3");
System.out.println("LinkedHashMap: " + linkedHashMap);

    String value = linkedHashMap.get("two");
    System.out.println("Value of key 'two': " + value);

    System.out.println("LinkedHashMap after get 'two': " + linkedHashMap);
}
}


In this example, we create a LinkedHashMap and add three key-value pairs to it. We then retrieve the value associated with the key “two”, which causes the entry for “two” to be moved to the end of the linked list. We then print the contents of the LinkedHashMap before and after the get operation.

The output of this program is as follows:

LinkedHashMap: {one=1, two=2, three=3}
Value of key 'two': 2
LinkedHashMap after get 'two': {one=1, three=3, two=2}


As we can see, the order of the entries in the LinkedHashMap reflects the order in which they were inserted, and the order is updated when an entry is retrieved.

Conclusion

A LinkedHashMap is a useful tool for managing key-value pairs in Java when the order in which the pairs were inserted is important. By maintaining a linked list of the entries in the map, a LinkedHashMap allows entries to be retrieved and added in their insertion order, and allows the most recently accessed entries to be quickly retrieved.

It is worth noting that using a LinkedHashMap can add some overhead to the application, as the linked list must be maintained and updated whenever entries are added, removed, or accessed.