Skip to content
Related Articles

Related Articles

How to Print all Keys of the LinkedHashMap in Java?
  • Last Updated : 17 Dec, 2020

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]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :