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 Print all Keys of the LinkedHashMap in Java?
Next article icon

How to Print all Keys of the LinkedHashMap in Java?

Last Updated : 17 Dec, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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  : Value-5      Key- 29 : Value-13      Key- 14 : Value-10      Key- 34 : Value-2      Key- 55 : Value-6    Output: 1, 29, 14, 34, 55

Method 1: Use for-each loop to iterate through LinkedHashMap. For each iteration, we print the respective key using getKey() method.

for(Map.Entry<Integer,Integer>ite : LHM.entrySet())      System.out.print(ite.getKey()+", ");

Example 1:

Java
// Java program to print all keys of the LinkedHashMap  import java.util.*; import java.io.*;  class GFG {     public static void main(String[] args)     {           // create a linkedhashmap         LinkedHashMap<Integer, Integer> LHM             = new LinkedHashMap<>();            // Add mappings         LHM.put(1, 5);         LHM.put(29, 13);         LHM.put(14, 10);         LHM.put(34, 2);         LHM.put(55, 6);            // print keys using getKey() method         for (Map.Entry<Integer, Integer> ite :              LHM.entrySet())             System.out.print(ite.getKey() + ", ");     } } 

Output
1, 29, 14, 34, 55,

Example 2:

Java
// Java program to print all keys of the LinkedHashMap  import java.util.*; import java.io.*;  class GFG {     public static void main(String[] args)     {         // create a linkedhashmap         LinkedHashMap<String, String> LHM             = new LinkedHashMap<>();          // Add mappings         LHM.put("Geeks", "Geeks");         LHM.put("for", "for");         LHM.put("Geeks", "Geeks");          // print keys using getKey() method         for (Map.Entry<String, String> ite : LHM.entrySet())             System.out.print(ite.getKey() + ", ");     } } 

Output
Geeks, for,

Method 2: (Using keySet() method)

Syntax:

hash_map.keySet()

Parameters: The method does not take any parameters.

Return Value: The method returns a set having the keys of the hash map.

Java
import java.io.*; import java.util.*;  class GFG {     public static void main(String[] args)     {         // create an instance of linked hashmap         LinkedHashMap<String, String> lhm             = new LinkedHashMap<String, String>();          lhm.put("One", "Geeks");         lhm.put("Two", "For");         lhm.put("Three", "Geeks");          // get all keys using the keySet method         Set<String> allKeys = lhm.keySet();          // print keys         System.out.println(allKeys);     } } 

Output
[One, Two, Three]

Next Article
How to Print all Keys of the LinkedHashMap in Java?

S

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

Similar Reads

    How to Get All the Values of the LinkedHashMap in Java?
    LinkedHashMap is a predefined class in Java that is similar to HashMap, contains key and its respective value, unlike HashMap. In LinkedHashMap insertion order is preserved. The task is to get all the values present in our LinkedHashMap that is linked with their respective key. Use Iteration or pred
    4 min read
    How to Add Key-Value pairs to LinkedHashMap in Java?
    LinkedHashMap is a Hash table and linked list implementation of the Map interface. In LinkedHashMap order of key-value pair depends on the order in which keys were inserted into the map. Insertion order does not affect if a key is reinserted into the map. Example: Input: Key: 1 Value : 1221 Key: 2 V
    2 min read
    Sort LinkedHashMap by Keys in Java
    LinkedHashMap maintains insertion order. Convert LinkedHashMap into TreeMap and after that print keys of TreeMap which are sorted in nature. Example: Input: linkedHashMap = {{5,4}, {3,44}, {4,15}, {1,20}, {2,11}} Output: key -> 1 : value -> 20 key -> 2 : value -> 11 key -> 3 : value -
    2 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
    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
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