Let’s see about LinkedHashMap.
- LinkedHashMap is a class which extends HashMap and implements Map interface.
- LinkedHashMap uses node representation to store the elements.
- LinkedHashMap stores the elements in key and value pair.
- The key of LinkedHashMap can’t be duplicate whereas value can be duplicate.
- We can have null as key and value in LinkedHashMap.
- LinkedHashMap maintains insertion order which makes this differ from HashMap.
- The initial capacity and load factor is 16 and 0.75 for LinkedHashMap.
- LinkedHashMap methods are not synchronized.
Example of LinkedHashMap –
package linkedhashmap;
import java.util.*;
public class Example1 {
public static void main(String[] args) {
Map<Integer,String> linkedHashMapObj = new LinkedHashMap<>();
linkedHashMapObj.put(2, "ram");
linkedHashMapObj.put(8, "mohan");
linkedHashMapObj.put(3, "sohan");
linkedHashMapObj.put(4, "rahul");
linkedHashMapObj.put(9, "rohan");
linkedHashMapObj.put(0, "suresh");
linkedHashMapObj.put(1, "ganesh");
// we have keySet() method which returns Set
Set<Integer> setOfKey= linkedHashMapObj.keySet();
for(Integer k : setOfKey) {
System.out.println("key is - " + k +" " + "value is - "+linkedHashMapObj.get(k));
}
}
}
Output is –
key is – 2 value is – ram
key is – 8 value is – mohan
key is – 3 value is – sohan
key is – 4 value is – rahul
key is – 9 value is – rohan
key is – 0 value is – suresh
key is – 1 value is – ganesh
[stextbox id=’info’]In the above output, we can see LinkedHashMap maintaining insertion order.[/stextbox]


