LinkedHashMap in Java

LinkedHashMap in Java

LinkedHashMap extends HashMap class and implements Map interface, so it inherit features of HashMap along with some other features which distinguish it from HashMap. A LinkedHashMap is a combination of hash table and linked list. It maintains a doubly-linked list running through all of its entries to maintain insertion order.

Followings are the features of LinkedHashMap.

  1. It stores key-value pairs.
  2. It contains unique key.
  3. Similar to HashMap, it allows one null key and multiple null values.
  4. Insertion order is maintained.
  5. Performance is likely to be just slightly below that of HashMap, due to the added expense of maintaining the linked list with one exception, Iteration over the LinkedHashMap requires time proportional to the size of the map, regardless of its capacity. While, Iteration over a HashMap is likely to be more expensive, requiring time proportional to its capacity.
  6. It is not synchronized.

LinkedHashMap provides following constructors.

[table “” not found /]

Example of LinkedHashMap.

package com.code2succeed.collection;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class LinkedHashMapExample {
	public static void main(String[] args) {
		LinkedHashMap linkedHashMap = new LinkedHashMap();
		linkedHashMap.put(1, "New York");
		linkedHashMap.put(3, "Paris");
		linkedHashMap.put(2, "London");
		linkedHashMap.put(4, "Mumbai");
		Iterator<Entry> iterator = linkedHashMap.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry entry = (Map.Entry);
			System.out.println("Key : " + entry.getKey() + " Value :" + entry.getValue());


Key : 1 Value :New York
Key : 3 Value :Paris
Key : 2 Value :London
Key : 4 Value :Mumbai

Leave a Reply

Your email address will not be published. Required fields are marked *