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
- map.values() will return a Collection of the map’s values.
- map.keySet() will return a Set of the map’s keys.
we can generate List from Map in several ways-
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());
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.
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]
- Google Maps in Android
- Base conversion in Java
- Conversion of Array To ArrayList in Java
- Widening Primitive Conversion in Java
- Array to ArrayList Conversion in Java
- Type conversion in Java with Examples
- Data Conversion Using valueOf() method in Java
- ArrayList to Array Conversion in Java : toArray() Methods
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
- Program to convert List of String to List of Integer in Java
- Program to convert List of Integer to List of String in Java
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Min and Max in a List in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.