Open In App

How to iterate LinkedHashMap in Java?

Last Updated : 25 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

How to Iterate LinkedHashMap in Reverse Order in Java?
The LinkedHashMap is used to maintain an order of elements inserted into it. It provides where the elements can be accessed in their insertion order. A LinkedHashMap contains values based on the key. It implements the Map interface and extends the HashMap class. It contains only unique elements or mappings. Syntax of Linkedhashmap public class Link
6 min read
Iterate through LinkedHashMap using an Iterator in Java
LinkedHashMap is a pre-defined class in java like HashMap. The only difference between LinkedHashMap and HashMap is LinkedHashMap preserve insertion order while HashMap does not preserve insertion order. The task is to iterate through a LinkedHashMap using an Iterator. We use the Iterator object to iterate through a LinkedHashMap. Example Input: Ke
2 min read
LinkedHashMap containsKey() Method in Java with Examples
The java.util.LinkedHashMap.containsKey() method is used to check whether a particular key is being mapped into the LinkedHashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map.Syntax: Linked_Hash_Map.containsKey(key_element) Parameters: The method takes just one parameter key_element that refe
2 min read
LinkedHashMap clear() Method in Java
The java.util.LinkedHashMap.clear() is an inbuilt method of LinkedHashMap class in Java and is used to clear all the elements or the mappings from the denoted LinkedHashMap. The map will become empty after the operation is performed. Syntax: Linked_Hash_Map.clear() Parameters: The method does not take any parameters. Return Value: The method does n
2 min read
LinkedHashMap get() Method in Java with Examples
In Java, get() method of LinkedHashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. --&gt; java.util Package --&gt; LinkedHashMap Class --&gt; get() Method Syntax: Linked_Hash_Map.get(Object key_element) Parameter: One parameter
2 min read
LinkedHashMap removeEldestEntry() Method in Java
The java.util.LinkedHashMap.removeEldestEntry() method in Java is used keep a track of whether the map removes any eldest entry from the map. So each time a new element is added to the LinkedHashMap, the eldest entry is removed from the map. This method is generally invoked after the addition of the elements into the map by the use of put() and put
3 min read
LinkedHashMap and LinkedHashSet in Java
The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order. Example
3 min read
Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
Given a string str containing only lowercase characters. The task is to print the characters along with their frequencies in the order of their occurrence in the given string.Examples: Input: str = "geeksforgeeks" Output: g2 e4 k2 s2 f1 o1 r1Input: str = "helloworld" Output: h1 e1 l3 o2 w1 r1 d1 Approach: Traverse the given string character by char
2 min read
How to Print all Mappings of the LinkedHashMap in Java?
The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. LinkedHashMap in Java is an implementation that combines HashTable and LinkedList implementation. It implements the Map interface. The key-value pairs of LinkedHashMap have a predictable order of iteration. We can use the entrySet
3 min read
How to Print all Keys of the LinkedHashMap in Java?
LinkedHashMap is a predefined class in Java that is similar to HashMap, contains a key and its respective value. Unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to print all the Keys present in our LinkedHashMap in java. We have to iterate through each Key in our LinkedHashMap and print It. Example : Input : Key- 1 : Valu
2 min read