LinkedHashMap in Java

LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order. Few important features of LinkedHashMap are as follows:

  • A LinkedHashMap contains values based on the key. It implements the Map interface and extends HashMap class.
  • It contains only unique elements (See this for details)..
  • It may have one null key and multiple null values (See this for details).
  • It is same as HashMap with additional feature that it maintains insertion order. For example, when we ran the code with HashMap, we got different oder of elements (See this).


LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();

Constructors in LinkedHashMap:
The LinkedHashMap accepts five types of constructors:

  1. LinkedHashMap(): This is used to construct a default LinkedHashMap constuctor.
  2. LinkedHashMap(int capacity): It is used to initialize a particular LinkedHashMap with a specified capacity.
  3. LinkedHashMap(Map m_a_p): It is used to initialize a particular LinkedHashMap with the elements of the specified map.
  4. LinkedHashMap(int capacity, float fillRatio): It is used to initialize both the capacity and fill ratio for a LinkedHashMap.
  5. LinkedHashMap(int capacity, float fillRatio, boolean Order): This constructor is also used to initialize both the capacity and fill ratio for a LinkedHashMap along with whether to follow the insertion order or not.
    • True is passed for last access order.
    • False is passed for insertion order.

Basic Operations of LinkedHashMap class:

// Java program to demonstrate working of LinkedHashMap
import java.util.*;

public class BasicLinkedHashMap
    public static void main(String a[])
        LinkedHashMap<String, String> lhm =
                       new LinkedHashMap<String, String>();
        lhm.put("one", "");
        lhm.put("two", "");
        lhm.put("four", "");

        // It prints the elements in same order 
        // as they were inserted    

        System.out.println("Getting value for key 'one': " 
                                       + lhm.get("one"));
        System.out.println("Size of the map: " + lhm.size());
        System.out.println("Is map empty? " + lhm.isEmpty());
        System.out.println("Contains key 'two'? "+ 
        System.out.println("Contains value 'practice.geeks"
        +"'? "+ lhm.containsValue("practice"+
        System.out.println("delete element 'one': " + 

Getting value for key 'one':
Size of the map: 3
Is map empty? false
Contains key 'two'? true
Contains value ''? true
delete element 'one':

Methods in LinkedHashMap:

  1. void clear(): This method is used to remove all the mappings from the map.
  2. boolean containsKey(Object key): This method is used to returns true if a specified element is mapped by one or more keys.
  3. Object get(Object key): The method is used to retrieve or fetch the value mapped by the specified key.
  4. protected boolean removeEldestEntry(Map.Entry eldest): The method is used to return true when the map removes its eldest entry from the map.


This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Improved By : Chinmoy Lenka

Practice Tags :

Recommended Posts:

2 Average Difficulty : 2/5.0
Based on 9 vote(s)

User Actions