Updates
  • Starting New Weekday Batch for Full Stack Java Development on 15 September 2025 @ 02:00 PM to 04:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM
Join Course

Java Full Stack Developer Training Center in Noida

LinkedHashMap

LinkedHashMap is a class which is available in java.util package. java.util.LinkedHashMap extends the java.util.HashMap class and implements the java.util.Map interface.

Important point of LinkedHashMap:

1. LinkedHashMap uses a Node representation to store its elements.
2. It stores data in the form of a Key-Value pair.
3. In LinkedHashMap, we can map different types of Keys and Values.
4. Duplicity is not allowed in Keys, but Values can be duplicated.
5. We can store null values as both keys and values.
6. Elements cannot be accessed randomly from the LinkedHashMap; it works on sequential access.
7. It stores data in the order of addition.
8. None of the methods of the java.util.LinkedHashMap class are synchronized.
9. Because the LinkedHashMap class extends the java.util.HashMap, and the HashMap class implements the Cloneable and Serializable interfaces, LinkedHashMap is also eligible for Cloning and Serialization.

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

public class JTC {
   public static void main(String[] args) {
      LinkedHashMap map = new LinkedHashMap();
      map.put((byte) 101, (byte) 201);
      map.put((short) 202, (short) 202);
      map.put((int) 303, (int) 303);
      map.put((long) 404, (long) 404);
      map.put(12.34f, 67.89f);
      map.put(34.56, -678.90);
      map.put(true, false);
      map.put('K', 'V');
      map.put("key", "value");
      map.put(null, null);
      map.put(new JTC(), new JTC());
      Set set = map.entrySet();
      Iterator iterator = set.iterator();
      while (iterator.hasNext()) {
         System.out.println(iterator.next());
      }
   }
}
    101=-55
    202=202
    303=303
    404=404
    12.34=67.89
    34.56=-678.9
    true=false
    K=V
    key=value
    null=null
    JTC@15db9742=JTC@6d06d69c
            

In LinkedHashMap, we have a feature that is not present in HashMap. When we add an element to LinkedHashMap, it is stored in the same order in which we followed during the addition of elements, unlike HashMap.

Important Constructor of java.util.LinkedHashMap class:

1. public java.util.LinkedHashMap(): It constructs an empty LinkedHashSet with an initial capacity of 16 and a default load factor of 0.75.

2. public java.util.LinkedHashMap(int): It constructs an empty LinkedHashMap with the specified initial capacity and a load factor of 0.75. It throws a java.lang.IllegalArgumentException if a negative argument is passed.

3. public java.util.LinkedHashMap(int, float): It creates an empty LinkedHashMap with the specified initial capacity and load factor. It throws an IllegalArgumentException if the initial capacity is negative or the load factor is non-positive.

4. public java.util.LinkedHashMap(java.util.Map): It creates a LinkedHashMap containing all the mappings of the specified map. The initial capacity will be sufficient to hold the mappings of the specified map, and the load factor will be 0.75. It throws a NullPointerException when the specified map is null.

Internal Working of LinkedHashMap:

In the last article, we explored the internal workings of HashMap. The internal working of LinkedHashMap is almost the same as HashMap, with the only difference being that when we add an element into LinkedHashMap, it is stored as an object of the java.util.LinkedHashMap$Entry class, which is a static inner class of java.util.LinkedHashMap.

static class Entry<K,V> extends HashMap.Node<K,V> { 
    Entry<K,V> before, after; 
    Entry(int hash, K key, V value, Node<K,V> next) { 
    super(hash, key, value, next); 
    } 
}

It shows LinkedHashMap stores the data using Doubly-Linked-List.

Here Key contains the Key object and Value contains the Value object and Before contains the referent the previous Node and After contains the reference of the next Node.