LinkedHashMap is an implementation of java.util.Map interface and is actually a combination of a linked list and a hash table.The main difference among LinkedHashMap and the other implementations of Map interface is that it preserves the insertion order of elements.In other words we will get the key/value pairs in the order that were inserted in the LinkedHashMap.As an example letâ??s compare a LinkedHashMap with a HashMap.

import java.util.HashMap;
import java.util.LinkedHashMap;


public class LinkedHashMapTest {

	public static void main(String[] args) {
		
		
		HashMap hashMap=new HashMap();
		hashMap.put(2, "Second");
		hashMap.put(3, "third");
		hashMap.put(1, "First");
		
		System.out.println("HASH MAP:"+hashMap);
		
		LinkedHashMap linkedHashMap=new LinkedHashMap();
		linkedHashMap.put(3, "third");
		linkedHashMap.put(1, "First");
		linkedHashMap.put(2, "Second");
		
		System.out.println("LINKED HASH MAP:"+linkedHashMap);
		
		
	}
}

Running the above snippet we will get:

HASH MAP:{1=First, 2=Second, 3=third}
LINKED HASH MAP:{3=third, 1=First, 2=Second}

Another nice feature of the LinkedHashMap is that we can choose between insertion order and access order.If we choose access order means that the key/value pairs will be ordered from the least recently accessed to most recently accessed element.For example if we run the below snippet:

public class LinkedHashMapTest {

	public static void main(String[] args) {
		
		
	
		
		LinkedHashMap linkedHashMap=new LinkedHashMap(10, 1.1F, true);
		linkedHashMap.put(3, "third");
		linkedHashMap.put(1, "First");
		linkedHashMap.put(2, "Second");
		
		linkedHashMap.get(1);
		
		System.out.println("LINKED HASH MAP:"+linkedHashMap);
		
		
	}
}
The ordering of the elements will change and we will get:
LINKED HASH MAP:{3=third, 2=Second, 1=First}