Tutorials

Java Collection Framework


LinkedHashMap


LinkedHashMap class extends HashMap class and implements Map interface.

LinkedHashMap is almost similar to the HashMap except it maintains the order of objects insertion into it.

 

Declaration of LinkedHashMap class 


public class LinkedHashMap extends HashMap implements Map  

Important points on LinkedHashMap

  • LinkedHashMap stores data as key, value pair.
  • LinkedHashMap maintains the insertion order of the objects that is stored.
  • All the keys in LinkedHashMap must be unique.

Create LinkedHashMap in java

We can use four different constructors to create LinkedHashMap as shown below:

  • LinkedHashMap myMap = new LinkedHashMap();
  • LinkedHashMap myMap = new LinkedHashMap(int initialCapacity);
  • LinkedHashMap myMap = new LinkedHashMap(Map m);
  • LinkedHashMap myMap = new LinkedHashMap(int initialCapacity, float loadFactor);

Simple Example


package LinkedHashMap;

import java.util.LinkedHashMap;

public class SimpleLHM {

      public static void main(String[] args) {

            //create HashMap in java
            LinkedHashMap javaRadarMap = new LinkedHashMap();

            //add elements to HashMap
            javaRadarMap.put(1, "Mukesh");
            javaRadarMap.put(2, "Puja");
            javaRadarMap.put(3, "Kavya");
            javaRadarMap.put(4, "Shweta");
            javaRadarMap.put(5, "Rajneesh");
            javaRadarMap.put(6, "Mithilesh");
        
            System.out.println(javaRadarMap);

      }

}

OUTPUT

{1=Mukesh, 2=Puja, 3=Kavya, 4=Shweta, 5=Rajneesh, 6=Mithilesh}



Please Share this page
Views : 104
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!