Linked Hashmap Example in Java
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
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
import java.util.LinkedHashMap;imports a class used later in the program. -
import java.util.Map;imports a class used later in the program. -
Map linkedHashMap = new LinkedHashMap();updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall 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
.javafilename. - Forgetting semicolons at the end of statements.