You are reading the article Java 8 List To Map updated in September 2023 on the website Nhahang12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Java 8 List To Map
Introduction to Java 8 List to MapJava 8 list to map conversion is a common task; we can say that list is a collection of child interfaces. It is nothing but the ordered collection of objects from which we are storing the duplicate values. Lists preserve insertion order, allowing us to insert elements and access positions. We are implementing the list interface with stack classes, linked lists, array lists, and stack.
The mapping between value and key is represented by the java util map interface. A common task in java programming is to convert a list to a map. List and map are 2 different data structures, each with its own set of properties. For example, the list interface in Java allows duplicate elements, but the key in a map is unique.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The key value is duplicate, but the duplicate key causes a problem in Java 8. So, by handling the duplicate values, we can say that the list with duplicates is not directly converted into the map. The list is an ordered collection, but the map does not take that guarantee until we use a linked hash map, which keeps order insertion, or a tree map, which keeps mapping in sorted order.
Key Takeaways
The map interface is not collection interface subtypes, so it will be different from other collection types.
To preserve the list items order in a map, we need to pass another parameter to the method of Collectors.toMap, which was deciding what type of map we are using.
How to Convert List to Map in Java 8?The below example shows how we can convert the list to map by using the object of list method as follows. To convert the list into a map by using the object of list we need to follow the below approach as follows:
Get the list that we are converting from list to map.
Create the empty map.
Iterate it through the items into the list and add the same to the map.
Return formed map.
The below example shows how we can convert the list into map by using the object of list as follows:
Code:
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.HashMap; class Stud { private Integer stud_id; private String stud_name; public Stud (Integer stud_id, String stud_name) { this.stud_id = stud_id; this.stud_name = stud_name; } public Integer getId () { return stud_id; } public String getName() { return stud_name; } } public class Main { public static void main(String[] args) { lt.add (new Stud (11, "ABC")); lt.add (new Stud (12, "PQR")); lt.add (new Stud (13, "XYZ")); for (Stud st : lt) { map.put (st.getId (), st.getName ()); } System.out.println ("Map : " + map); } }Output:
The below example shows how we can convert the list into map by using collectos.groupingby.
Below is the approach to convert the list to map by using collectos.groupingby as follows:
Get the list which we convert into map.
Convert the list into list.stream method.
Create the map by using collectos.groupingby method.
Collect the formatted map by using stream.collect() method.
Return the map which was formed.
The below example shows how we can convert the list to map by using collectos.groupingby method as follows:
Code:
import java.util.*; import java.util.stream.Collectors; class stud { private Integer stud_id; private String stud_name; public stud(Integer stud_id, String stud_name) { this.stud_id = stud_id; this.stud_name = stud_name; } public Integer getId() { return stud_id; } public String getName() { return stud_name; } } public class stud1 { public static void main(String[] args) { st.add(new stud(21, "ABC")); st.add(new stud(22, "PQR")); st.add(new stud(23, "XYZ")); multimap = st .stream() .collect( Collectors .groupingBy( stud::getId, Collectors .mapping( stud::getName, Collectors .toList()))); System.out.println("Map = " + multimap); } }Output:
Collectors.toMap ()The Collectors.toMap method is used to convert the list into a map. This method is including the list creation of specified objects, and we are using Collectors.toMap method to convert the list into a map.
We are using the following approach while using Collectors.toMap method as follows:
Get the list that we are converting into map.
Convert the list into the stream by using List.stream method.
Create the map by using Collectors.toMap method.
Collect the map, which was formatted by using stream.collect method.
Return the map which was formed.
The below example shows how we can use the method of Collectors.toMap to convert the list into the map as follows:
Code:
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.stream.Collectors; class stud { private Integer stud_id; private String stud_name; public stud(Integer stud_id, String stud_name) { this.stud_id = stud_id; this.stud_name = stud_name; } public Integer getId() { return stud_id; } public String getName() { return stud_name; } } public class stud1 { public static void main(String[] args) { st.add (new stud(31, "PQR")); st.add (new stud(32, "ABC")); st.add (new stud(33, "XYZ")); map = st.stream () .collect( Collectors .toMap ( stud::getId, stud::getName, (A, B) LinkedHashMap::new)); map.forEach ( } }Output:
Duplicated KeyWhile handling the duplicate key, we need to pass the third argument, which was informing the toMap method, which values considering the duplicate items.
The below example shows how we can handle the duplicate key in java 8 at the time of converting the list to map as follows.
Code:
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class stud { public static void main(String[] args) { st.add(new stud(51, "ABC")); st.add(new stud(52, "PQR"); st.add(new stud(53, "XYZ")); Collectors.toMap(stud::getId, stud::getName, ) ); System.out.println("Result : " + res); } }Output:
Sort and CollectThe below example shows how we can use the sort and collect method in java 8 at the time of converting list to a map as follows:
Code:
import java.util.*; import java.util.stream.Collectors; public class stud { public static void main(String[] args) { st.add(new stud(91, "ABC")); st.add(new stud(92, "PQR")); st.add(new stud(93, "XYZ")); st.add(new stud(94, "MNP")); Map res = st.stream() .collect( Collectors.toMap( stud::getId, stud::getName, LinkedHashMap::new )); System.out.println("Result : " + res); } }Output:
Examples of Java 8 List to MapIn the below example, we are using collectors.groupingBy approach as follows.
Code:
import java.util.*; import java.util.stream.Collectors; class emp { private Integer emp_id; private String emp_name; public emp(Integer emp_id, String emp_name) { this.emp_id = emp_id; this.emp_name = emp_name; } public Integer getId() { return emp_id; } public String getName() { return emp_name; } } public class emp1 { public static void main(String[] args) { ep.add(new emp(101, "ABC")); ep.add(new emp(102, "PQR")); ep.add(new emp(103, "XYZ")); ep.add(new emp(104, "MNP")); multimap = ep .stream() .collect( Collectors .groupingBy( emp::getId, Collectors .mapping( emp::getName, Collectors .toList()))); System.out.println("Emp details = " + multimap); } }Output:
In the below example, we are using object of list approach as follows:
Code:
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.HashMap; class emp { private Integer emp_id; private String emp_name; public emp (Integer emp_id, String emp_name) { this.emp_id = emp_id; this.emp_name = emp_name; } public Integer getId () { return emp_id; } public String getName() { return emp_name; } } public class emp1 { public static void main(String[] args) { ep.add (new emp (11, "ABC")); ep.add (new emp (12, "PQR")); ep.add (new emp (13, "XYZ")); for (emp ep1 : ep) { map.put (ep1.getId (), ep1.getName ()); } System.out.println ("Emp : " + map); } }Output:
ConclusionThe java util map interface is representing the mapping between the value and the key. To convert list in map is a common task in java programming. Java 8 list to map conversion is a common task; we can say that list is a collection of child interfaces. It is nothing but the ordered collection of objects from which we are storing the duplicate values.
Recommended ArticlesThis is a guide to Java 8 List to Map. Here we discuss the introduction, how to convert a list to a map in Java 8, collectors.toMap () and examples. You may also have a look at the following articles to learn more –
You're reading Java 8 List To Map
Update the detailed information about Java 8 List To Map on the Nhahang12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!