Open In App

Iterate through LinkedHashMap using an Iterator in Java

Last Updated : 04 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:    Key - 2  : Value - 6
    Key - 3  : Value - 4
    Key - 6  : Value - 5
    Key - 4  : Value - 10
    Key - 5  : Value - 6

Output:

Key = Value
2   = 6
3   = 4
6   = 5
4   = 10
5   = 6

Algorithm : 

1. Create a LinkedHashMap and add key, value pairs.

2. we convert our LinkedHashMap to entrySet using,

Set s = lhm.entrySet();

3. we define iterator for our set.    

Iterator it=s.iterator();

4. Using while loop we iterate through our linkedHashMap. 

while(it.hasNext())                      
     System.out.println(it.next()); 

Implementation

Java




// Java program to Iterate through LinkedHashMap using an
// Iterator
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // create a linkedhashmap
        LinkedHashMap<Integer, Integer> lhm
            = new LinkedHashMap<>();
  
        // add mappings
        lhm.put(2, 6);
        lhm.put(3, 4);
        lhm.put(6, 8);
        lhm.put(4, 10);
        lhm.put(5, 6);
  
        // create an entryset
        Set s = lhm.entrySet();
  
        // create an iterator
        Iterator it = s.iterator();
  
        // iterate an print the mappings
        System.out.println("key=Value");
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
}


Output

key=Value
2=6
3=4
6=8
4=10
5=6

Time Complexity: O(n).



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
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 LinkedH
2 min read
How to Iterate Over a HashSet Without an Iterator in Java?
In Java, we can iterate over a HashSet without using an Iterator. For this, we need two methods. We can directly loop through the elements of the HashSet. In this article, we will discuss the two methods to iterate over a HashSet without using Iterator. Program to Iterate over a HashSet without an Iterator1. Using For-Each loopSyntax: for (Datatype
2 min read
Iterate through List in Java
Lists in java allow us to maintain an ordered collection of objects. Duplicate elements as well as null elements can also be stored in a List in Java. The List interface is a part of java.util package and it inherits the Collection interface. It preserves the order of insertion. There are several ways to iterate over List in Java. They are discusse
6 min read
Iterate Through Elements of LinkedHashSet in Java
The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. Example: Input: ["Geeks", "for", "Geeks"] Output: Geeks for Geeks Input: [9, 4, 6, 2, 8] Output: 9 4 6 2 8 Different Ways to Iterate LinkedHashSet Elements: Using the for
3 min read
How to Iterate through Collection Objects in Java?
Any group of individual objects which are represented as a single unit is known as the Collection of the objects. In Java, a separate framework named the “Collection Framework” has been defined in JDK 1.2 which holds all the collection classes and interface in it. The Collection interface (java.util.Collection) and Map interface (java.util.Map) are
3 min read
How to Iterate Through HashTable in Java?
HashTable is an underlying data structure where the insertion order in HashTable is not preserved, and it is based on the hashcode of keys. Duplicates keys are not allowed, but values can be duplicated. Heterogeneous objects are allowed for both keys and values. Value null is not allowed for both key and value otherwise we will get RunTimeException
8 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
Sort LinkedHashMap by Values using Comparable Interface in Java
The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. Assuming you have gone through LinkedHashMap in java and know about LinkedHashMap. Syntax: int compare(T obj) ; Illustration: Input : { GEEKS=1, geeks=3, for=2 } Output : { GEEKS=1, for=2, geeks=3 } Input : { 101 = 2, 102 = 9, 103
2 min read
Sort LinkedHashMap by Keys using Comparable Interface in Java
The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order. To sort LinkedHashMap by keys using the comparable interface in Java first, we
3 min read