Home
Java
LinkedHashMap Example
Updated Mar 21, 2023
Dot Net Perls
LinkedHashMap. In a HashMap, the order entries are added is not maintained—it is lost when they are put into the table. But with LinkedHashMap, a linked list maintains the order.
HashMap
To ensure we can access elements in the order we add them, LinkedHashMap is a good alternative to HashMap. It still has constant time lookup, but just has some overhead.
An example program. Let us examine this program. We import the LinkedHashMap in the same way we would import HashMap. We use diamond inference syntax in the same way as well.
Info We invoke put to add 3 entries to our LinkedHashMap. Then we use entrySet() to loop over them.
for
Next We print the key and value of each Entry to the console with System.out.println.
Console
Important Notice how the order of our loop is maintained when we access entrySet. The collection never forgot the ordering.
import java.util.LinkedHashMap; import java.util.Map.Entry; public class Program { public static void main(String[] args) { // Add 3 entries to a LinkedHashMap. LinkedHashMap<String, Integer> values = new LinkedHashMap<>(); values.put("bird", 10); values.put("frog", 20); values.put("snake", 25); // For LinkedHashMap, we can loop over the items in the order they were added. // ... A linked list keeps track of the order as we add them. for (Entry<String, Integer> pair : values.entrySet()) { System.out.println(pair.getKey() + "/" + pair.getValue()); } } }
bird/10 frog/20 snake/25
HashMap loop. Consider this version of the program. It uses HashMap instead of LinkedHashMap. The items are added in a certain order, and then loops over the entries.
Tip With HashMap (unlike LinkedHashMap) they are looped over in a different order. This program shows the change.
import java.util.HashMap; import java.util.Map.Entry; public class Program { public static void main(String[] args) { HashMap<String, Integer> values = new HashMap<>(); values.put("bird", 10); values.put("frog", 20); values.put("snake", 25); for (Entry<String, Integer> pair : values.entrySet()) { System.out.println(pair.getKey() + "/" + pair.getValue()); } } }
frog/20 bird/10 snake/25
Some considerations. The LinkedHashMap is an ideal replacement for HashMap when we need to maintain the order of items we put into the table. If ordering is needed, prefer LinkedHashMap.
However If we do not need the order, HashMap is better because it will be slightly faster in most programs.
A simple review. The LinkedHashMap is a special kind of HashMap. It enforces a linked list data structure where the order is maintained. This helps if the time of an addition is important.
For most programs, HashMap is a better choice as it is simpler. It also has less conceptual complexity (it omits the "Linked" part). It is important to choose the correct collection.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 21, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen