How to create LinkedHashMap program in java

import java.util.*;

class LinkedHashMap_TechSarvam 
{
    public static void main(String args[]) 
    {
        // Creating a LinkedHashMap
        LinkedHashMap<Integer, String> hm = new LinkedHashMap<Integer, String>();

        // Adding elements to the LinkedHashMap
        hm.put(100, "Rajendra");
        hm.put(101, "Vijay");
        hm.put(102, "TechSarvam");

        // Iterating through the LinkedHashMap
        for (Map.Entry<Integer, String> m : hm.entrySet()) 
        {
            System.out.println(m.getKey() + " " + m.getValue());
        }
    }
}

Step-by-Step Explanation

1. importing the necessary Classes

import java.util.*;

To support using classes that are in utility library of java such as

LinkedHashMap as well as its nested class   Map.Entry by importing the necessary package.

2. Class Declaration

class LinkedHashMap_TechSarvam

There is a class, called LinkedHashMap_TechSarvam, which defines this class inside of which is the entry-point method for a program called main().

3. Main Method:

public static void main(String args[]) {

The main() method is the entry point for the Java program. It runs when the application is launched.

4. Declaring a LinkedHashMap Object:

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

  • A LinkedHashMap object hm is declared. LinkedHashMap is a subclass of HashMap that preserves the insertion order of the elements.
  • The map is parameterized with <Integer, String>, meaning it will store key-value pairs where the key is of type Integer and the value is of type String.

5. Adding Key-Value Pairs:

hm.put(100, “Rajendra”);

hm.put(101, “Vijay”);

hm.put(102, “TechSarvam”);

  • put() is the method that places a key-value pair into a LinkedHashMap.
  • The 100 key corresponds to the value “Rajendra”.
  • The 101 key corresponds to the value “Vijay”.
  • Key 102 is assigned with the value  “TechSarvam”.

6. Traversing the LinkedHashMap:

for (Map.Entry m : hm.entrySet()) {

      System.out.println(m.getKey() + ” ” + m.getValue());

  }

  • hm.entrySet(): Returns a Set of entries in the LinkedHashMap. An entry is an instance of Map.Entry and holds the key-value pair.
  • for (Map.Entry m : hm.entrySet()): This loop iterates through the set of entries, where each Map.Entry is referred to by m.
  • m.getKey(): Retrieves the key from the entry.
  • m.getValue(): Returns the value for a given key.
  • The System.out.println(m.getKey() + ” ” + m.getValue()); will print each key and its respective value on one line.

7. Output

The program outputs the key-value pairs stored in the LinkedHashMap. Since LinkedHashMap orders entries by the order of insertion, the output will be shown in the same order that entries were added to the map.

Sample Output:

100 Rajendra

 101 Vijay

 102 TechSarvam

Key Concepts:

1. LinkedHashMap:

  • A LinkedHashMap is an implementation of the Map interface that maintains the order of insertion. The order in which elements are added to the map is preserved during iteration.
  • In contrast with HashMap, which does not guarantee any fixed order of its contents, the insertion order or access order is remembered in LinkedHashMap, if the latter has been invoked.

2. put() Method:

The put() method can add a key-value pair into the map. When the given key already exists in the Map, the value will overwrite previous values.

3. Map.Entry:

A Map.Entry is an entry of a key-value pair in the map. It contains methods getKey() to get the key and getValue() to get the value.

4. entrySet() Method:

The entrySet() method returns a set of Map.Entry objects. These objects can be iterated to get keys and values.

5. Iteration:

The enhanced for loop (for (Map.Entry m : hm.entrySet())) iterates over the entries of the map. This is a straightforward and efficient means of traversing collections in Java.

Time Complexity:

  • Insertion (put()): Average case, O(1). In a LinkedHashMap, the entries are stored in a hash table; therefore, adding key-value pairs is a constant time operation.
  • Iteration (entrySet() and for-each loop): O(n), where n is the number of entries in the map.

Leave a Comment

Your email address will not be published. Required fields are marked *