LinkedHashMap Class In Java

The Java LinkedHashMap class is a hash table and doubly linked-based implementation of Java’s Map interface. It is more similar to the HashMap class, but the advantages of the HashMap class are in quick insertion, deletion, and search operation, but it does not store the elements in the insertion order. Still, if you use LinkedHashMap, you can access the elements in their insertion order.

Key Points of LinkedHashMap

  • It Stores the elements in the key-value pairs as HashMap.
  • It does not allow duplicate keys to be inserted.
  • It allows one null key and multiple Null values.
  • It stores the elements in the same order in which they have entered.
  • LinkedHashMap is not threaded like HashMap, but you can make it thread-safe externally.

How to Declare LinkedHashMap Class

You can declare the LinkedHashMap by using the below syntax:

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

Constructors in LinkedHashMap

The LinkedHashMap accepts five types of constructors:

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

LinkedHashMap Methods And Various Operations In Java: Different LinkedHashMap Methods Used in Java With Example?

package com.java.Softwaretestingblog;
import java.util.LinkedHashMap;
public class BasicLinkedHashMap {
   public static void main(String[] args) {
   // TODO Auto-generated method stub
   //www.softwaretestingblog.in
   LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
   lhm.put("one", "This is first element"); 
   lhm.put("two", "This is second element"); 
   lhm.put("four", "this element inserted at 3rd position"); 

   System.out.println(lhm); 
   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'? "+lhm.containsKey("two")); 
   System.out.println("Contains value 'This is first element'? " +lhm.containsValue("This is first element")); 
   System.out.println("delete element 'one': "+lhm.remove("one")); 
   System.out.println(lhm);
   }
}

Output:

{one=This is first element, two=This is second element, four=this element inserted at 3rd position}
Getting value for key 'one': This is first element
Size of the map: 3
Is map empty? false
Contains key 'two'? true
Contains value 'This is first element'? true
delete element 'one': This is first element
{two=This is second element, four=this element inserted at 3rd position}

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

Add Values In LinkedHashMap Java Program & Explanation?

package com.softwaretestingo.java.basics;
import java.util.LinkedHashMap;
public class MyMapValueCheck 
{
   public static void main(String[] args) 
   {
      LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
      lhm.put("one", "This is first element");
      lhm.put("two", "This is second element");
      lhm.put("four", "Element inserted at 3rd position");
      System.out.println("Map contains value 'This is first element'? "
            +lhm.containsValue("This is first element"));

   }
}

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment