- 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 Get a Value From LinkedHashMap by Index in Java?
LinkedHashMap is a generic class of Java Collection Framework that implements Map Interface. As the name suggests, it is a sub-class of the HashMap class and uses a doubly LinkedList to store the entries in insertion order. However, LinkedHashMap does not do any kind of indexing to store the entries, therefore, it is quite a challenge to get specified values from LinkedHashMap with the help of an index. The aim of this article is to explain how to find a value from LinkedHashMap using the index.
Java Program to Get a Value from LinkedHashMap by Index
Before jumping to the example program directly, let's understand a few more points on LinkedHashMap first:
LinkedHashMap
As we have discussed earlier the LinkedHashMap class extends HashMap class to implement Map Interface. Hence, it can use all the methods and also perform similar operations that a HashMap class is capable of. It maintains Key-Value pair of entries and the order in which they were inserted into the map. The Key is an object that is used to fetch and receive value associated with it. Also, whenever we return its element it will be printed in the insertion order.
The general syntax for LinkedHashMap is as follows:
Syntax
LinkedHashMap< TypeOfKey, TypeOfValue > nameOfMap = new LinkedHashMap<>();
In the above syntax,
TypeOfKey: Specify the datatype of Keys.
TypeOfValue: Specify the datatype of values that are going to be stored in map.
nameOfMap: Give a suitable name to your map.
Now, let's create Java Programs for finding a specified value from LinkedHashMap using Index.
Example 1
In the following example, we will use the iterator interface to get the specified value from LinkedHashMap by Index.
Approach
First, import the 'java.util' package so that we can access the functionality of Map.
Then, create a LinkedHashMap and store a few elements of specified type by using the built-in method 'put()'.
Define an iterator and store the values of LinkedHashMap to it.
Now, initialize a counter variable to iterate till the required index and another variable named 'value' to store the value of that index.
Use the while loop to extract the value from specified index.
import java.util.*; public class Example1 { public static void main(String[] args) { // Creating a LinkedHashMap LinkedHashMap<String, Integer> LinkHmap = new LinkedHashMap<>(); // storing elements to the map LinkHmap.put("TShirt", 59); LinkHmap.put("Trouser", 60); LinkHmap.put("Shirt", 45); LinkHmap.put("Watch", 230); LinkHmap.put("Shoes", 55); // to print all entries System.out.println("All entries from LinkedHashMap: " + LinkHmap); // Defining an iterator Iterator<Integer> iter = LinkHmap.values().iterator(); // Initializing a counter int cntr = 0; // Initializing a variable for storing the value int value = 0; // iterating using while loop while (iter.hasNext() && cntr < 2) { // to get the next value and increment the counter value = iter.next(); cntr++; } // Printing the result System.out.println("The value at index 2 from LinkedHashMap is: " + value); } }
Output
All entries from LinkedHashMap: {TShirt = 59, Trouser = 60, Shirt = 45, Watch = 230, Shoes = 55} The value at index 2 from LinkedHashMap is: 60
Example 2
This is another Java program that demonstrates the use of for-each loop to get a value from LinkedHashMap by index.
Approach
Follow the first two steps of the previous example.
Then, initialize an 'index' variable to specify the required index.
Now, take a for-each loop that will iterate through the keys of LinkedHashMap and increment the index by 1 during each iteration.
When the index gets incremented to 3 the if block will get executed and print the result.
import java.util.*; public class Example2 { public static void main(String[] args) { // Creating a LinkedHashMap LinkedHashMap<String, Integer> LinkHmap = new LinkedHashMap<>(); // storing elements to the map LinkHmap.put("TShirt", 59); LinkHmap.put("Trouser", 60); LinkHmap.put("Shirt", 45); LinkHmap.put("Watch", 230); LinkHmap.put("Shoes", 55); // to print all entries for (String unKey : LinkHmap.keySet()) { System.out.println("Item: " + unKey + ", Quantity: " + LinkHmap.get(unKey)); } // Initializing the index int index = 0; // iterating using for-each loop for (String unKey : LinkHmap.keySet()) { index++; // incrementing the counter if(index == 3) { // printing the value at index 3 System.out.println("The value at index 3 from LinkedHashMap is: " + LinkHmap.get(unKey)); } } } }
Output
Item: TShirt, Quantity: 59 Item: Trouser, Quantity: 60 Item: Shirt, Quantity: 45 Item: Watch, Quantity: 230 Item: Shoes, Quantity: 55 The value at index 3 from LinkedHashMap is: 45
Conclusion
We started this article by defining the Java LinkedHashMap class and in the next section, we discussed two approaches to finding a value from LinkedHashMap with the help of an index. Also, we discovered how to create a LinkedHashMap collection.