Skip to main content

Linked Hashmap Example in Java

3 min read Updated May 29, 2026
Share:
On this page (10sections)

Introduction

Linked HashMap is a classic Java console program that demonstrates the concept with complete source code and sample output. The Collections Framework provides ArrayList, HashMap, HashSet and related data structures.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

HashMap uses a hash table. The hashes of the keys are used to find the values in various buckets. LinkedHashMap extends this by creating a doubly linked list between the elements. This allows the elements to be accessed in the order in which they were inserted into the map.

Syntax

Map Variable_name = new LinkedHashMap();

Linked HashMap Example Program

import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapDemo {
  public static void main(String[] args) {
        Map linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(1, new String("Samsung"));
        linkedHashMap.put(2, new String("Mi"));
        linkedHashMap.put(3, new String("Toshiba"));
        linkedHashMap.put(4, new String("HCL"));
        linkedHashMap.put(5, new String("Wipro"));
        System.out.println("Contents of LinkedHashMap : " + linkedHashMap);
        System.out.println("\nValues of map after iterating over it : ");
        for (Integer key : linkedHashMap.keySet()) {
            System.out.println(key + ":\t" + linkedHashMap.get(key));
        }
        System.out.println("\nThe size of the LinkedHashMap is : " + linkedHashMap.size());
        System.out.println("\nIs LinkedHashMap empty? : " + linkedHashMap.isEmpty());
        System.out.println("\nLinkedHashMap contains 2 as key? : " + linkedHashMap.containsKey(2));
        System.out.println("LinkedHashMap contains HCL as value? : " + linkedHashMap.containsValue("HCL"));
        System.out.println("\nRemove entry for key 3 : " + linkedHashMap.remove(3));
        System.out.println("Content of LinkedHashMap after removing key 2: " + linkedHashMap);
        linkedHashMap.clear();
        System.out.println("\nContent of LinkedHashMap after clearing: " + linkedHashMap);
    }
}

Sample Output

Contents of LinkedHashMap : {1=Samsung, 2=Mi, 3=Toshiba, 4=HCL, 5=Wipro}

Values of map after iterating over it :
1:      Samsung
2:      Mi
3:      Toshiba
4:      HCL
5:      Wipro

The size of the LinkedHashMap is : 5

Is LinkedHashMap empty? : false

LinkedHashMap contains 2 as key? : true
LinkedHashMap contains HCL as value? : true

Remove entry for key 3 : Toshiba
Content of LinkedHashMap after removing key 2: {1=Samsung, 2=Mi, 4=HCL, 5=Wipro}

Content of LinkedHashMap after clearing: {}

When to use

Use this linked hashmap example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.util.LinkedHashMap; imports a class used later in the program.

  3. import java.util.Map; imports a class used later in the program.

  4. Map linkedHashMap = new LinkedHashMap(); updates a variable used in the calculation or output.

  5. A println / print call writes text to the console — part of the sample output below.

  6. A println / print call writes text to the console — part of the sample output below.

  7. A println / print call writes text to the console — part of the sample output below.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

  • Use meaningful variable and class names that describe their purpose.
  • Compile and run the program locally — modify values to see how output changes.
  • Read compiler errors carefully; they usually point to the exact line to fix.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Linked HashMap program demonstrate?
It shows how to implement linked hashmap in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials