Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
LinkedHashMap
2.1.
Syntax 
3.
Features
4.
Constructors of LinkedHashMap class
5.
Methods of Java LinkedHashMap class
6.
Example
6.1.
Code 1
6.2.
Code 2
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

LinkedHashMap

gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

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

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.

Syntax 

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
Bootcamp

Features

  • 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.

Example

Code 1

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

Output

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>();        
       
        map.put(101,"Saalim");    
        map.put(102,"CN");    
        map.put(103,"Maaz");
         
        System.out.println("Before invoking remove() method: "+map);   
    
        map.remove(102);
        System.out.println("After invoking remove() method: "+map);
    }      
}   

Output

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
WeakHashMap
Next article
IdentityHashMap in Java
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass