Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Constructors of LinkedHashMap class
Methods of Java LinkedHashMap class
Code 1
Code 2
Frequently Asked Questions
Key Takeaways
Last Updated: Mar 27, 2024


Master Power BI using Netflix Data
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM


HashMap has been part of Java's collection since its version 1.2. This class can be found in Java.util package. It stores the data in the Key-value pair form. You can access the data with the help of an index of another type. One object will use the key (index), and another will operate as an integer.

Also read, Duck Number in Java and  Hashcode Method in Java


LinkedHashMap is like HashMap with an add-on feature of keeping elements inserted in order. It comes with the advantage of quick insertion, searching, and deletion. It also comes with the benefit of maintaining the insertion order. You can say it's a combination of Hashtable and LinkedList.


public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job


  • A LinkedHashMap consists of values based on the keys, which can be implemented by extending the HashMap class.
  • It consists of unique keys.
  • It might contain one null key with multiple null values.
  • It comes with an additional feature of maintaining the insertion order.

Constructors of LinkedHashMap class

  • LinkedHashMap(): This is used to create the default LinkedHashMap constructor.
 LinkedHashMap<K,V> llhm = new LinkedHashMap<K,V>();


  • LinkedHashMap(int capacity): This hashmap will create LinkedHashMap with user-defined capacity.
 LinkedHashMap<K,V> llhm = new LinkedHashMap<K,V>(int cap);


  • LinkedHashMAp(Map<? Extends K,? Extends V> map): It is used to declare a LinkedHashMap with elements of the specified map.
 LinkedHashMap<K, V> llhm = new LinkedHashMap<K, V>(Map<? extends K,​? extends V> map); 


  • LinkedHashMap(int cap, float ratio): It is used to create a particular LinkedHashMap with user-defined capacity and ratio. 
 LinkedHashMap<K, V>llhm = new LinkedHashMap<K, V>(int cap, float ratio);


  • LinkedHashMap(int capacity, float Ratio, boolean Order): This type will create a constructor that initializes both capacity and ratio. Here the order is the access order for insertion.
 LinkedHashMap<K, V> llhm = new LinkedHashMap<K, V>(int capacity, float fillRatio, boolean Order);

Methods of Java LinkedHashMap class

  • V get(Object key): This function will return the specified key's value.
  • Void clear(): It helps to remove all the key-value pairs from the map.
  • boolean containsValue(Object value): It returns true if the map maps one or more keys to the specific matter. 
  • Set<Map.Entry<K, V>> entrySet(): This function will return the set view of the mappings which are contained in the map.
  • Set<K>keySet(): This will return a set idea of the keys contained in the set.


Code 1

import java.util.*;  
public class Ex1{  
     public static void main(String args[]){  
        LinkedHashMap<Integer,String> hm=new LinkedHashMap<Integer,String>();  
        for(Map.Entry m:hm.entrySet()){  
            System.out.println(m.getKey()+" "+m.getValue());  


100 Mohammad
101 Saalim
102 Mohtashim

Try it on java online compiler.
In this example, we have used a LinkedHashMap, and we used certain functions which are put. This function helps us to put the value in the HashMap. After placing the value, we print the values entered in the map.

Code 2

import java.util.*;  
public class Ex2 {  
    public static void main(String args[]) {  
        Map<Integer,String> map=new LinkedHashMap<Integer,String>();        
        System.out.println("Before invoking remove() method: "+map);   
        System.out.println("After invoking remove() method: "+map);


Before invoking remove() method: {101=Saalim, 102=CN, 103=Maaz}
After invoking remove() method: {101=Saalim, 103=Maaz}

In this code, we have added specific names and some numbers. We have used the put function to add the names and their values. After putting the names, we used a function remove to remove the specific entry. 

Also see,  Swap Function in Java

Frequently Asked Questions

  1. What is the purpose of LinkedHashMap?
    LinkedHashMap is a Hash table and an implementation of LinkedList of the Map interface, along with the predictable iteration order. 
  2. Is LinkedHashMap synchronized?
    It's not synchronized, just like HashMap, so if you are going to access it from different threads, at least one of these threads is likely to change.

Key Takeaways

In this blog, we have covered the introduction of LinkedHashMap. We also discussed LinkedHashMap with its syntax. We also covered its key features and covered different constructors that can be used in LinkedHashMap and various methods that can be helpful. We also took several examples of LinkedHashMap by showing the usage of their functions. 

Recommended Readings:

If you want to learn more about this concept and practice more problems, please visit our website, Problems on HashMap.

Previous article
Next article
IdentityHashMap in Java
Live masterclass