Open In App
Related Articles

How to Print all Keys of the LinkedHashMap in Java?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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]


Last Updated : 17 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads