How to Get All the Values of the LinkedHashMap 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. It maintains Key-Value pair of entries. The Key is an object that is used to fetch and receive value associated with it. Hence, we can use this key along with 'get()' method to get all the values from LinkedHashMap. The aim of this article is to explain different ways to print all values of the LinkedHashMap.

Java Program to Get all the Value of the LinkedHashMap

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 stores the elements of the map in LinkedList in the order in which they are inserted, therefore, 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 printing all the values from LinkedHashMap.

Example 1

The following example demonstrates how to use the for-each loop to get all values from LinkedHashMap.

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()'.

  • Moving further, initialize an 'index' variable to specify the indices.

  • Now, take a for-each loop that will iterate through the keys of LinkedHashMap and increment the index by 1 during each iteration. Also, use the 'get()' method to print the values.

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 items from LinkedHashMap :");
      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()) {
         // printing the result
         System.out.println("The value at index " + index + " from LinkedHashMap is: " + LinkHmap.get(unKey)); 
         index++; // incrementing the index
      }
   }
}

Output

All items from LinkedHashMap :
Item: TShirt, Quantity: 59
Item: Trouser, Quantity: 60
Item: Shirt, Quantity: 45
Item: Watch, Quantity: 230
Item: Shoes, Quantity: 55
The value at index 0 from LinkedHashMap is: 59
The value at index 1 from LinkedHashMap is: 60
The value at index 2 from LinkedHashMap is: 45
The value at index 3 from LinkedHashMap is: 230
The value at index 4 from LinkedHashMap is: 55

Example 2

In the following example, we will use the iterator interface to get all values of LinkedHashMap.

Approach

  • Follow the first two steps of the previous example.

  • Define an iterator and store the values of LinkedHashMap to it.

  • Then, initialize an 'index' variable to specify the indices.

  • Use the while loop to iterate till the end of available values and extract the values. Also, increment the 'index' by 1 during each iteration.

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
      System.out.println("All items from LinkedHashMap :");
      for (String unKey : LinkHmap.keySet()) {
         System.out.println("Item: " + unKey + ", Quantity: " + LinkHmap.get(unKey));
      }
      // Defining an iterator
      Iterator<Integer> iter = LinkHmap.values().iterator();
      // Initializing the index
      int index = 0;
      // iterating using while loop
      while (iter.hasNext()) {
         // to get the next value and increment the index
         System.out.println("The value at index " + index + " from LinkedHashMap is: " + iter.next()); 
         index++;
      }
   }
}

Output

All items from LinkedHashMap :
Item: TShirt, Quantity: 59
Item: Trouser, Quantity: 60
Item: Shirt, Quantity: 45
Item: Watch, Quantity: 230
Item: Shoes, Quantity: 55
The value at index 0 from LinkedHashMap is: 59
The value at index 1 from LinkedHashMap is: 60
The value at index 2 from LinkedHashMap is: 45
The value at index 3 from LinkedHashMap is: 230
The value at index 4 from LinkedHashMap is: 55

Conclusion

In this article, we have learned the LinkedHashMap and how to create one. The aim of this article was to find different approaches to printing all the values from LinkedHashMap. For this operation, we have seen two example programs.

Updated on: 20-Jul-2023

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements