Java LinkedHashMap is an implementation of hash table and linked list of the Map interface. Unlike HashMap, LinkedHashMap maintains the order of its elements. That means it returns the elements in the same order they were inserted into it.

Another difference between HashMap and LinkedHashMap is that it maintains doubly linked list internally to link all of its elements. So it guarantees to maintain the insertion order of the elements.

HashMap does not maintain the order of the elements and if there is such requirement, you can create a LinkedHashMap object from the existing map object using LinkedHashMap constructor which accepts a map object argument like given below

LinkedHashMap linkedMap = new LinkedHashMap(existingHashMapObject);

There is also a special LinkedHashMap which returns the elements in the order they were last accessed. To create such LinkedHashMap object, you can use below given constructor.

public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)

This kind of map object is particularly useful to cache the frequently accessed data.

Like HashMap, LinkedHashMap also provides all Map operations. It also allows null elements. LinkedHashMap provides constant time performance for basic operations like add, remove and contains as well.

LinkedHashMap is not synchronized. That means if multiple threads are simultaneously accessing the LinkedHashMap and modifying it, the code must handle the synchronization explicitly. You can also get synchronized map instance from LinkedHashMap using synchronizedMap method of Collections class.

Below given are some of the Java LinkedHashMap examples which shows how to use LinkedHashMap in Java.

Java LinkedHashMap Examples