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
Â
Âimport
java.util.ArrayList;
import
java.util.List;
import
java.util.Map;
import
java.util.HashMap;
Â
Â// create a list
class
Student {
Â
ÂÂ Â Â Â
// id will act as Key
   Â
private
Integer id;
Â
ÂÂ Â Â Â
// name will act as value
   Â
private
String name;
Â
ÂÂ Â Â Â
// create curstuctor for reference
   Â
public
Student(Integer id, String name)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// assign the value of id and name
       Â
this
.id = id;
       Â
this
.name = name;
   Â
}
Â
ÂÂ Â Â Â
// return private variable id
   Â
public
Integer getId()
   Â
{
       Â
return
id;
   Â
}
Â
ÂÂ Â Â Â
// return private variable name
   Â
public
String getName()
   Â
{
       Â
return
name;
   Â
}
}
Â
Â// main class and method
public
class
GFG {
Â
ÂÂ Â Â Â
// main Driver
   Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// create a list
       Â
List<Student>
           Â
lt =
new
ArrayList<Student>();
Â
ÂÂ Â Â Â Â Â Â Â
// add the member of list
       Â
lt.add(
new
Student(
1
,
"Geeks"
));
       Â
lt.add(
new
Student(
2
,
"For"
));
       Â
lt.add(
new
Student(
3
,
"Geeks"
));
Â
ÂÂ Â Â Â Â Â Â Â
// create map with the help of
       Â
// Object (stu) method
       Â
// create object of Map class
       Â
Map<Integer, String> map =
new
HashMap<>();
Â
ÂÂ Â Â Â Â Â Â Â
// 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
Â
Âimport
java.util.ArrayList;
import
java.util.LinkedHashMap;
import
java.util.List;
import
java.util.stream.Collectors;
Â
Â// create a list
class
Student {
Â
ÂÂ Â Â Â
// id will act as Key
   Â
private
Integer id;
Â
ÂÂ Â Â Â
// name will act as value
   Â
private
String name;
Â
ÂÂ Â Â Â
// create curstuctor for reference
   Â
public
Student(Integer id, String name)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// assign the value of id and name
       Â
this
.id = id;
       Â
this
.name = name;
   Â
}
Â
ÂÂ Â Â Â
// return private variable id
   Â
public
Integer getId()
   Â
{
       Â
return
id;
   Â
}
Â
ÂÂ Â Â Â
// return private variable name
   Â
public
String getName()
   Â
{
       Â
return
name;
   Â
}
}
Â
Â// main class and method
public
class
GFG {
Â
ÂÂ Â Â Â
// main Driver
   Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// create a list
       Â
List<Student> lt =
new
ArrayList<>();
Â
ÂÂ Â Â Â Â Â Â Â
// add the member of list
       Â
lt.add(
new
Student(
1
,
"Geeks"
));
       Â
lt.add(
new
Student(
2
,
"For"
));
       Â
lt.add(
new
Student(
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
Â
Âimport
java.util.*;
import
java.util.stream.Collectors;
Â
Â// create a list
class
Student {
Â
ÂÂ Â Â Â
// id will act as Key
   Â
private
Integer id;
Â
ÂÂ Â Â Â
// name will act as value
   Â
private
String name;
Â
ÂÂ Â Â Â
// create curstuctor for reference
   Â
public
Student(Integer id, String name)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// assign the value of id and name
       Â
this
.id = id;
       Â
this
.name = name;
   Â
}
Â
ÂÂ Â Â Â
// return private variable id
   Â
public
Integer getId()
   Â
{
       Â
return
id;
   Â
}
Â
ÂÂ Â Â Â
// return private variable name
   Â
public
String getName()
   Â
{
       Â
return
name;
   Â
}
}
Â
Â// main class and method
public
class
GFG {
Â
ÂÂ Â Â Â
// main Driver
   Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// create a list
       Â
List<Student> lt =
new
ArrayList<Student>();
Â
ÂÂ Â Â Â Â Â Â Â
// add the member of list
       Â
lt.add(
new
Student(
1
,
"Geeks"
));
       Â
lt.add(
new
Student(
1
,
"For"
));
       Â
lt.add(
new
Student(
2
,
"Geeks"
));
       Â
lt.add(
new
Student(
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]}