Program to Convert List to Map in Java
The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.
The java.util.Map interface represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore it behaves a bit different from the rest of the collection types.

Example:
Input: List : [1="1", 2="2", 3="3"]
Output: Map : {1=1, 2=2, 3=3}
Input: List : [1="Geeks", 2="for", 3="Geeks"]
Output: Map : {1=Geeks, 2=for, 3=Geeks}
Below are various ways to convert List to Map in Java. For this, it is assumed that each element of the List has an identifier which will be used as a key in the resulting Map.
- Using by object of list:
Approach:
- Get the List to be converted into Map
- Create an empty Map
- Iterate through the items in the list and add each of them into the Map.
- Return the formed Map
// Java program for list convert in map// with the help of Object methodimportjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.util.HashMap;// create a listclassStudent {// id will act as KeyprivateInteger id;// name will act as valueprivateString name;// create curstuctor for referencepublicStudent(Integer id, String name){// assign the value of id and namethis.id = id;this.name = name;}// return private variable idpublicInteger getId(){returnid;}// return private variable namepublicString getName(){returnname;}}// main class and methodpublicclassGFG {// main Driverpublicstaticvoidmain(String[] args){// create a listList<Student>lt =newArrayList<Student>();// add the member of listlt.add(newStudent(1,"Geeks"));lt.add(newStudent(2,"For"));lt.add(newStudent(3,"Geeks"));// create map with the help of// Object (stu) method// create object of Map classMap<Integer, String> map =newHashMap<>();// put every value list to Mapfor(Student stu : lt) {map.put(stu.getId(), stu.getName());}// print mapSystem.out.println("Map : "+ map);}}chevron_rightfilter_noneOutput:Map : {1=Geeks, 2=For, 3=Geeks} - Using Collectors.toMap() method: This method includes creation of a list of the student objects, and uses Collectors.toMap() to convert it into a Map.
Approach:- Get the List to be converted into Map
- Convert the List into stream using List.stream() method
- Create map with the help of Collectors.toMap() method
- Collect the formed Map using stream.collect() method
- Return the formed Map
// Java program for list convert in map// with the help of Collectors.toMap() methodimportjava.util.ArrayList;importjava.util.LinkedHashMap;importjava.util.List;importjava.util.stream.Collectors;// create a listclassStudent {// id will act as KeyprivateInteger id;// name will act as valueprivateString name;// create curstuctor for referencepublicStudent(Integer id, String name){// assign the value of id and namethis.id = id;this.name = name;}// return private variable idpublicInteger getId(){returnid;}// return private variable namepublicString getName(){returnname;}}// main class and methodpublicclassGFG {// main Driverpublicstaticvoidmain(String[] args){// create a listList<Student> lt =newArrayList<>();// add the member of listlt.add(newStudent(1,"Geeks"));lt.add(newStudent(2,"For"));lt.add(newStudent(3,"Geeks"));// create map with the help of// Collectors.toMap() methodLinkedHashMap<Integer, String>map = lt.stream().collect(Collectors.toMap(Student::getId,Student::getName,(x, y)-> x +", "+ y,LinkedHashMap::new));// print mapmap.forEach((x, y) -> System.out.println(x +"="+ y));}}chevron_rightfilter_noneOutput:1=Geeks 2=For 3=Geeks
- Creating MultiMap using Collectors.groupingBy():
Approach:
- Get the List to be converted into Map
- Convert the List into stream using List.stream() method
- Create map with the help of Collectors.groupingBy() method
- Collect the formed Map using stream.collect() method
- Return the formed Map
// Java program for list convert in map// with the help of Collectors.groupingBy() methodimportjava.util.*;importjava.util.stream.Collectors;// create a listclassStudent {// id will act as KeyprivateInteger id;// name will act as valueprivateString name;// create curstuctor for referencepublicStudent(Integer id, String name){// assign the value of id and namethis.id = id;this.name = name;}// return private variable idpublicInteger getId(){returnid;}// return private variable namepublicString getName(){returnname;}}// main class and methodpublicclassGFG {// main Driverpublicstaticvoidmain(String[] args){// create a listList<Student> lt =newArrayList<Student>();// add the member of listlt.add(newStudent(1,"Geeks"));lt.add(newStudent(1,"For"));lt.add(newStudent(2,"Geeks"));lt.add(newStudent(2,"GeeksForGeeks"));// create map with the help of// Object (stu) method// create object of Multi Map class// create multimap and store the value of listMap<Integer, List<String> >multimap = lt.stream().collect(Collectors.groupingBy(Student::getId,Collectors.mapping(Student::getName,Collectors.toList())));// print the multiMapSystem.out.println("MultiMap = "+ multimap);}}chevron_rightfilter_noneOutput:MultiMap = {1=[Geeks, For], 2=[Geeks, GeeksForGeeks]}
Recommended Posts:
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- Program to Convert Set to List in Java
- Program to convert Array to List in Java
- Program to Convert List to Stream in Java
- Program to Convert a Vector to List in Java
- Convert List to Set in Java
- Convert an Iterator to a List in Java
- Convert a String to a List of Characters in Java
- Convert List of Characters to String in Java
- Convert String into comma separated List in Java
- Program to convert a Map to a Stream in Java
- Program to convert Array to Set in Java
- Program to convert a Set to Stream in Java using Generics
- Program to convert ArrayList to LinkedList 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 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.
Improved By : nidhi_biet



