linked hashmap and its features
A LinkedHashMap in Java is a subclass of the HashMap class and implements the Map interface. It combines the features of a HashMap and a LinkedList, providing predictable iteration order and fast access. LinkedHashMap maintains a doubly-linked list of the entries, which defines the order of insertion. In addition, it also allows for iteration in the order in which entries were inserted into the map (insertion-order) or in the order of access (access-order).

Table of Contents
Features of LinkedHashMap:
- 1. Preserves Insertion Order: LinkedHashMap maintains the order in which elements were inserted into the map. It uses a doubly-linked list to keep track of the insertion order of elements.
- 2. Fast Access and Lookup: LinkedHashMap provides fast access to elements similar to HashMap. The get() and put() operations have constant-time complexity on average.
- 3. Iterable: LinkedHashMap allows iteration over its entries in the order they were inserted or in the order of access (if access-order mode is enabled).
- 4. Supports Access-Order Mode: In addition to insertion-order mode, LinkedHashMap also supports access-order mode. In access-order mode, the iteration order is based on the order in which entries were accessed (recently accessed elements come first).
- 5. No Duplicate Keys: Like other Map implementations, LinkedHashMap does not allow duplicate keys. If an attempt is made to add a duplicate key, the new value will overwrite the existing value associated with that key.
- 6. Null Keys and Values LinkedHashMap allows null keys and null values. It can store and retrieve null keys and values like other Map implementations.
```java
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapExample {
public static void main(String[] args) {
// Create a LinkedHashMap
Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
// Add key-value pairs to the LinkedHashMap
linkedHashMap.put("Apple", 10);
linkedHashMap.put("Banana", 20);
linkedHashMap.put("Cherry", 15);
// Get the value associated with a key
int value = linkedHashMap.get("Banana");
System.out.println("Value associated with 'Banana': " + value);
// Remove a key-value pair from the map
linkedHashMap.remove("Cherry");
// Print all key-value pairs in the LinkedHashMap
System.out.println("Key-Value Pairs:");
for (Map.Entry<String, Integer> entry : linkedHashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
```
In this example:
- We create a LinkedHashMap and add some key-value pairs to it.
- We retrieve the value associated with the key “Banana” and remove the key-value pair associated with the key “Cherry” from the map.
- Finally, we iterate over all key-value pairs in the LinkedHashMap and print them out.
LinkedHashMap is commonly used in scenarios where maintaining insertion order is important, such as building a cache or preserving the order of configuration settings. It provides predictable iteration order and fast access to elements, making it suitable for various data storage and manipulation tasks.