- 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
Conversion of Java Maps to List
At first, let us create a Java Map and initialize −
Map<Integer, String> map = new HashMap<>(); map.put(1, "Tom"); map.put(2, "John"); map.put(3, "Kevin"); map.put(4, "Jacob"); map.put(5, "Ryan");
Now, convert the Map to List −
ArrayList<Integer> key = new ArrayList<Integer>(map.keySet()); ArrayList<String> value = new ArrayList<String>(map.values());
Example
Following is the program to convert Maps to List in Java −
import java.util.HashMap; import java.util.ArrayList; import java.util.Map; public class Demo { public static void main(String args[]) { Map<Integer, String> map = new HashMap<>(); map.put(1, "Tom"); map.put(2, "John"); map.put(3, "Kevin"); map.put(4, "Jacob"); map.put(5, "Ryan"); map.put(6, "Bradley"); map.put(7, "Andy"); map.put(8, "Tim"); System.out.println("HashMap = " + map); ArrayList<Integer> key = new ArrayList<Integer>(map.keySet()); ArrayList<String> value = new ArrayList<String>(map.values()); System.out.println("
List (Map Keys) = "+key); System.out.println("List (Map Values) = "+value); } }
Output
HashMap = {1=Tom, 2=John, 3=Kevin, 4=Jacob, 5=Ryan, 6=Bradley, 7=Andy, 8=Tim} List (Map Keys) = [1, 2, 3, 4, 5, 6, 7, 8] List (Map Values) = [Tom, John, Kevin, Jacob, Ryan, Bradley, Andy, Tim]
Advertisements