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 methodÂÂimportjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.util.HashMap;ÂÂ// create a listclassStudent {   Â// id will act as Key   ÂprivateInteger id;   Â// name will act as value   ÂprivateString name;   Â// create curstuctor for reference   ÂpublicStudent(Integer id, String name)   Â{       Â// assign the value of id and name       Âthis.id = id;       Âthis.name = name;   Â}   Â// return private variable id   ÂpublicInteger getId()   Â{       Âreturnid;   Â}   Â// return private variable name   ÂpublicString getName()   Â{       Âreturnname;   Â}}ÂÂ// main class and methodpublicclassGFG {   Â// main Driver   Âpublicstaticvoidmain(String[] args)   Â{       Â// create a list       ÂList<Student>           Âlt =newArrayList<Student>();       Â// add the member of list       Âlt.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 class       ÂMap<Integer, String> map =newHashMap<>();       Â// put every value list to Map       Âfor(Student stu : lt) {           Âmap.put(stu.getId(), stu.getName());       Â}       Â// print map       ÂSystem.out.println("Map : "+ map);   Â}}Output: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() methodÂÂimportjava.util.ArrayList;importjava.util.LinkedHashMap;importjava.util.List;importjava.util.stream.Collectors;ÂÂ// create a listclassStudent {   Â// id will act as Key   ÂprivateInteger id;   Â// name will act as value   ÂprivateString name;   Â// create curstuctor for reference   ÂpublicStudent(Integer id, String name)   Â{       Â// assign the value of id and name       Âthis.id = id;       Âthis.name = name;   Â}   Â// return private variable id   ÂpublicInteger getId()   Â{       Âreturnid;   Â}   Â// return private variable name   ÂpublicString getName()   Â{       Âreturnname;   Â}}ÂÂ// main class and methodpublicclassGFG {   Â// main Driver   Âpublicstaticvoidmain(String[] args)   Â{       Â// create a list       ÂList<Student> lt =newArrayList<>();       Â// add the member of list       Âlt.add(newStudent(1,"Geeks"));       Âlt.add(newStudent(2,"For"));       Âlt.add(newStudent(3,"Geeks"));       Â// create map with the help of       Â// Collectors.toMap() method       ÂLinkedHashMap<Integer, String>           Âmap = lt.stream()                     Â.collect(                         ÂCollectors                             Â.toMap(                                 ÂStudent::getId,                                 ÂStudent::getName,                                 Â(x, y)                                     Â-> x +", "+ y,                                 ÂLinkedHashMap::new));       Â// print map       Âmap.forEach(           Â(x, y) -> System.out.println(x +"="+ y));   Â}}Output: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() methodÂÂimportjava.util.*;importjava.util.stream.Collectors;ÂÂ// create a listclassStudent {   Â// id will act as Key   ÂprivateInteger id;   Â// name will act as value   ÂprivateString name;   Â// create curstuctor for reference   ÂpublicStudent(Integer id, String name)   Â{       Â// assign the value of id and name       Âthis.id = id;       Âthis.name = name;   Â}   Â// return private variable id   ÂpublicInteger getId()   Â{       Âreturnid;   Â}   Â// return private variable name   ÂpublicString getName()   Â{       Âreturnname;   Â}}ÂÂ// main class and methodpublicclassGFG {   Â// main Driver   Âpublicstaticvoidmain(String[] args)   Â{       Â// create a list       ÂList<Student> lt =newArrayList<Student>();       Â// add the member of list       Âlt.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 list       ÂMap<Integer, List<String> >           Âmultimap = lt                          Â.stream()                          Â.collect(                              ÂCollectors                                  Â.groupingBy(                                      ÂStudent::getId,                                      ÂCollectors                                          Â.mapping(                                              ÂStudent::getName,                                              ÂCollectors                                                  Â.toList())));       Â// print the multiMap       ÂSystem.out.println("MultiMap = "+ multimap);   Â}}Output:MultiMap = {1=[Geeks, For], 2=[Geeks, GeeksForGeeks]}



