How to iterate LinkedHashMap in Java?

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:

  1. Using keySet() and get() Method
  2. Using entrySet() and Iterator

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.

  • Through keySet() method we will obtain a set having keys of the map.
  • And then after running a loop over this set, we can obtain each key and its value using get() method.

Java




// 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));
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā }
}


Output

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());
Ā Ā Ā Ā }
}


Output

LinkedHashMap entries : 
One=First element
Two=Second element
Three=Third element

Related Articles

Leave a Reply

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

Back to top button