Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to iterate LinkedHashMap in Java?
Next article icon

How to iterate LinkedHashMap in Java?

Last Updated : 25 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
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

Next Article
How to iterate LinkedHashMap in Java?

L

lavishgarg26
Improve
Article Tags :
  • Java
  • Java Programs
  • Technical Scripter 2020
  • Java-Collections
  • Java-LinkedHashMap
Practice Tags :
  • Java
  • Java-Collections

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 m
    6 min read
    How to Merge Two LinkedHashMaps in Java?
    In Java, LinkedHashMap is a class that extends HashMap and maintains the order of elements based on the order of insertion. Merging two LinkedHashMaps involves combining their key-value pairs while ensuring that the order is preserved. In, this article we will explore different approaches to merging
    3 min read
    How to Check if LinkedHashMap is Empty 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
    2 min read
    Java Program to Iterate LinkedHashSet Elements
    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. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements
    3 min read
    How to Convert LinkedHashMap to List in Java?
    LinkedHashMap is predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, in LinkedHashMap insertion order is preserved. We to convert LinkedHashMap to ArrayList. A Map store data in pair of Key and Value while converting a LinkedHashMAp to ArrayLis
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences