LinkedHashMap class in Java
Map is an object that maps keys to values. A map cannot contain duplicate keys.
LinkedHashMap class is a hash table and linked list implementation of the Map interface. Here, the elements are ordered based on the order they were inserted.
This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion order).
LinkedHashMap class permits null
values and the null
key.
Java provides the LinkedHashMap class as part of the Java Collections Framework. For an overview of the Java Collections Framework, check out my post Overview of the Java Collections Framework.
The following example illustrates the LinkedHashMap class.
Example
Output
As you can see from the above output, LinkedHashMap preserved the insertion order of the elements.
The above program showed the use of some of the common methods of the LinkedHashMap class. For a complete list of all the available methods, refer the official docs.
Now since you know how to use the LinkedHashMap class, check out other implementations of the Map interface: