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);}}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() 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));}}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() 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);}}Output:MultiMap = {1=[Geeks, For], 2=[Geeks, GeeksForGeeks]}




