How to Convert all LinkedHashMap Values to a List in Java

Riya
Last Updated: May 13, 2022

Introduction-  

This article will discuss how to convert all LinkedHashMap values to a list in java. LinkedListHashMap is used to implement map data structure which stores key value pairs. And list in java provides interface to store ordered collection of elements. So,they are two different data structures which stores data in two entirely different manner. And in this article we have discussed how we can convert values of all the key value pairs stored in LinkedHashMap to a list in java.

 

Let’s understand this with an example:

Suppose we have a LinkedHashMap which has following data stored as key value pairs-

(1,5), (3,7), (6,9)

And we are going to discuss how we can convert this LinkedHashMap values to a list in java which will have - 5, 7, 9 in sorted order.


                                                                           

Method:

We can convert all LinkedHashMap values to a list using java.util.LinkedHashMap.values() which don’t take any parameter and return a Collection view of the values in the HashMap.

 

Syntax for converting all LinkedHashMap values to a list:

LinkedListHashMap_name.values() 

where “LinkedHashMap_name” is the name of the LinkedHashMap.

Java code:

// Java program to Convert all LinkedHashMap values to a List
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class LinkedHashMapToList {
public static void main(String[] args)
{

// Create a LinkedHashMap
LinkedHashMap<Integer, Integer> linkedHashMapExample = new LinkedHashMap<Integer, Integer>();


// Adding key value pairs in the LinkedHashMap
linkedHashMapExample.put(1, 5);
linkedHashMapExample.put(3, 7);
linkedHashMapExample.put(6, 9);


// Method to convert all LinkedHashMap values to List
List<Integer> convertedValuesList = new ArrayList<Integer>(linkedHashMapExample.values());


// Printing the list created by converting all LinkedHashMap values to List
System.out.println("List created after conversion of all LinkedHashMap values to a list contains the following elements:");
for (Integer value : convertedValuesList) {
System.out.println(value);
}
}
}

 

Output:
List created after conversion of all LinkedHashMap values to a list contains the following elements:
5
7
9

 

Algorithm Complexity: 

Time Complexity: O(N), where ‘N’ is the number of elements in the LinkedHashMap

Space Complexity: O(N), where ‘N’ is the number of elements in the LinkedHashMap

Frequently asked questions-

  1. What is a LinkedHashMap in java?

LinkedHashMap in java is a data structure used to implement map interface which stores data in key value pairs.  

2. What is List in java?

 In Java, list is child interface of “Collection” which is used to store objects in sorted order and can contain duplicate values also.

Key takeaways-

This article discussed how to convert all LinkedHashMap values to a list in java, and its time and space complexities. If you want to read more blogs related to java, then you can visit codestudio.

 

If you think that this blog helped you, then share it with your friends!. Refer to the DSA Java course for more information.
 

Until then, All the best for your future endeavors, and Keep Coding.

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think