Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

How to Convert all LinkedHashMap Values to a List in Java

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM
Linked List

Introduction

This article will discuss how to convert all LinkedHashMap values to a list in java. LinkedListHashMap is used to implement a map data structure that stores key-value pairs. And the list in java provides an interface to store an ordered collection of elements. So, they are two different data structures that stores data in two entirely different manners. 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 that has the following data stored as key-value pairs-

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

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

Solution

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.

 

The 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

Check out this problem - Find Duplicate In Array

Must Read Difference between ArrayList and LinkedList

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

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.  

What is a List in java?

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

Conclusion

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 Coding Ninjas Studio.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

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

Topics covered
1.
Introduction
2.
Solution
2.1.
Java code
2.2.
Algorithm Complexity
3.
Frequently Asked Questions
3.1.
What is a LinkedHashMap in java?
3.2.
What is a List in java?
4.
Conclusion