Last Updated : 23 Jul, 2025
LinkedHashMap class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted.
There are basically two ways to iterate over LinkedHashMap:
Method 1: Iterating LinkedHashMap using keySet() and get() Method
Syntax:
linked_hash_map.keySet()
Parameters: The method does not take any parameter.
Return Value: The method returns a set having the keys of the LinkedHashMap.
// Java Program to iterate through LinkedHashMap using
// keySet() and get() Method
import java.util.LinkedHashMap;
import java.util.Set;
public class GFG {
public static void main(String a[])
{
// making the object of LinkedHashMap
LinkedHashMap<String, String> linkedHashMap
= new LinkedHashMap<String, String>();
// adding the elements in linkedHashMap
linkedHashMap.put("One", "First element");
linkedHashMap.put("Two", "Second element");
linkedHashMap.put("Three", "Third element");
Set<String> keys = linkedHashMap.keySet();
// printing the elements of LinkedHashMap
for (String key : keys) {
System.out.println(key + " -- "
+ linkedHashMap.get(key));
}
}
}
One -- First element Two -- Second element Three -- Third element
Method 2: Iterating LinkedHashMap using entrySet() and Iterator
Syntax:
Linkedhash_map.entrySet()
Parameters: The method does not take any parameter.
Return Value: The method returns a set having same elements as the LinkedHashMap.
Java
// Java Program to iterate over linkedHashMap using
// entrySet() and iterator
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
public class GFG {
public static void main(String[] args)
{
// Create a LinkedHashMap and populate it with
// elements
LinkedHashMap<String, String> linkedHashMap
= new LinkedHashMap<String, String>();
// adding the elements to the linkedHashMap
linkedHashMap.put("One", "First element");
linkedHashMap.put("Two", "Second element");
linkedHashMap.put("Three", "Third element");
// Get a set of all the entries (key - value pairs)
// contained in the LinkedHashMap
Set entrySet = linkedHashMap.entrySet();
// Obtain an Iterator for the entries Set
Iterator it = entrySet.iterator();
// Iterate through LinkedHashMap entries
System.out.println("LinkedHashMap entries : ");
while (it.hasNext())
// iterating over each element using it.next()
// method
System.out.println(it.next());
}
}
LinkedHashMap entries : One=First element Two=Second element Three=Third element
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4