- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Print all Keys of the LinkedHashMap in Java?
Java's LinkedHashMap data structure combines the strengths of a HashMap as well as of a doubly-linked list. It is one of the better options for situations requiring predictable iteration as it not only offers key-value mapping like a HashMap but also preserves the insertion order of components. There are times when we need to print every key found in a LinkedHashMap for multiple reasons, that include debugging, analysis and information display to users. In this post, we will examine two excellent techniques that let us print every key from a LinkedHashMap in Java, enabling us to effectively extract and visualize the key information.
keySet() method to access LinkedHashMap
The LinkedHashMap's keys are all contained in a Set view that is returned by the keySet() method. This implies that the original map will reflect any changes made to the set. We can iterate over the keys and carry out different actions based on our needs by acquiring the key set.
Syntax
The keys of a LinkedHashMap can be accessed and used effectively using the following way:-
Set<KeyType> keySet = linkedHashMap.keySet();
Approaches
To print all keys of the LinkedHashMap in Java, we can follow the two methods:
Utilizing the keySet() method.
Utilizing the forEach() method.
Let us look into both approaches:-
Approach-1: Utilizing the keySet() method.
The first way involves employing the LinkedHashMap class's keySet() method to get a Set view of all the keys. After that, we may go through this set again and print each key separately.
Algorithm
The steps to print all keys of the LinkedHashMap in Java are as follows:
Step-1: LinkedHashMap objects can be created.
Step-2: Add key-value pairs to the LinkedHashMap.
Step-3: Utilize the keySet() function to get the set of keys.
Step-4: Print each key as you iterate through the list of keys.
Example
import java.util.LinkedHashMap; import java.util.Map; public class Main { public static void main(String[] args) { //Create a new LinkedHashMap LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<>(); //Put values to the LinkedHashMap linkedHashMap.put(1, "Pen"); linkedHashMap.put(2, "Book"); linkedHashMap.put(3, "Pencil"); printKeys(linkedHashMap); } private static void printKeys(LinkedHashMap<Integer, String> map) { //Print the keys of the LinkedHashMap for (Integer key : map.keySet()) { System.out.println(key); } } }
Output
1 2 3
Approach-2: Utilizing the forEach() method
In the second strategy, the forEach() function from Java 8 is employed. With the help of this approach, we may repeatedly go over the LinkedHashMap's entries and take a certain action on each one. We can print the key for each entry in this situation. With Java 8's forEach() method, we may iterate through LinkedHashMap elements and carry out a specific action on each one. As an argument, it accepts a lambda expression that specifies the action to be taken on each item. Given that we can specify the desired operation within the lambda expression, this technique offers a clear and expressive way to interact with the keys (and values) of a LinkedHashMap.
Algorithm
The steps to print all keys of the LinkedHashMap in Java are as follows:
Step-1: LinkedHashMap objects can be constructed.
Step-2: Add key-value pairs to the LinkedHashMap.
Step-3: To loop through the LinkedHashMap's items, use the forEach() method.
Step-4: Extract and print the key for each entry.
Utilize the addAll() function to return the elements from the HashSet to the ArrayList.
Example
import java.util.LinkedHashMap; import java.util.Map; public class Main { public static void main(String[] args) { //Create a new LinkedHashMap LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<>(); //Add some values to it linkedHashMap.put(1, "Pen"); linkedHashMap.put(2, "Pencil"); linkedHashMap.put(3, "Book"); // Call the printKeys function //pass the LinkedHashMap for printing values printKeys(linkedHashMap); } private static void printKeys(LinkedHashMap<Integer, String> map) { //Print the keys with the help of forEach() method map.forEach((key, value) -> System.out.println(key)); } }
Output
1 2 3
Conclusion
In this article, we looked at two methods for printing every key from a Java LinkedHashMap. Understanding these procedures will enable you to effectively analyze, debug, or display the keys of your LinkedHashMap in your code. You can select the strategy that best meets your needs based on your coding preferences and the specifications of your project. These methods let you take advantage of the LinkedHashMap data structure's capability while quickly accessing and printing its keys.