Conversion of Java Maps to List

A Map is an object that maps keys to values or is a collection of attribute-value pairs. The list is an ordered collection of objects and List can contain duplicate values.
The map has two values (a key and value), while a List only has one value (an element). So we can generate two list – a list of values and list of keys from a Map.

Assuming map is the instance of Map

  1. map.values() will return a Collection of the map’s values.
  2. map.keySet() will return a Set of the map’s keys.

we can generate List from Map in several ways-
Methods 1:
We can convert Map keys to List of Keys by passing set of map keys generated by map.keySet() method to ArrayList Constructor Parameter.



List ListofKeys = new ArrayList(map.keySet());

We can convert Map keys to List of Values by passing collection of map values generated by map.values() method to ArrayList Constructor Parameter.

List Listofvalues= new ArrayList(map.values());

Methods 2:
You can also use Stream API to convert map keys and values to respective lists.

List ListofKeys = map.keyset().stream().
               collect(Collectors.toCollection(ArrayList::new));
List Listofvalues= map.values().stream().
               collect(Collectors.toCollection(ArrayList::new));

The stream() method returns a stream of the keys from the Set of the map keys returned by Map.keySet(). The collect() method of the Stream class is collects the stream of keys in a List.The Collectors.toCollection(ArrayList::new) passed to the collect() method to collect as new Arraylist. You can collect elements of Stream in a ArrayList, LinkedList, or any other List implementation.

Example: Given RollNo and Student Name of N students as input. First, we create a Map where Rollno is key because Rollno is unique and Name as Value for Map then convert this map to list of values and keys respectively. Where the generated list of keys contains RollNo of students and list of Values contains Name of Students.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert a map to list.
import java.util.*;
import java.util.stream.*;
  
public class MapToList {
  
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
  
        // create a Hashmap which maps rollno with student name
        Map<String, String> map = new HashMap<String, String>();
        System.out.println("Enter No of Students");
  
        // Take Input to Hashmap
        int noOfStudents = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < noOfStudents; i++) {
            String input = sc.nextLine();
            String[] studentdata = input.split(" ");
            String rollno = studentdata[0];
            String name = studentdata[1];
            map.put(rollno, name);
        }
  
        // create list of keys and values
        List<String> ListofKeys = null;
        List<String> ListofValues = null;
  
        // convert hashmap to list of keys and values
        ListofKeys = map.keySet().stream().collect(Collectors.
                                       toCollection(ArrayList::new));
        ListofValues = map.values().stream().collect(Collectors.
                                       toCollection(ArrayList::new));
  
        // print list of rollno and name of students
        System.out.println("List of RollNo of Students");
        System.out.println(ListofKeys.toString());
        System.out.println("List of Name of Students");
        System.out.println(ListofValues.toString());
    }
}

chevron_right


Input :
Enter No of Students 
5
101 aman
102 amar
103 suraj
104 pritam
Output :
List of RollNo of Students
[101, 102, 103, 104]
List of Name of Students
[aman, amar, suraj, pritam]

Reference:HashMap API Documentation, List API documentation



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.