60
Map of Map in Java
In Java, Map is an interface that maps keys to values. Sometimes it is required to implement Map of Map (nested Map).
Nested Map is used in many cases, such as storing students’ names with their Ids of different courses. In this case, we create a Map having a key, i.e., course name and value, i.e., another Map having a key, i.e., Id and value, i.e., the student’s name.
We should use the following steps to create a Map of Map in Java:
- Take the total number of students from the user for each course.
- Fill in the student’s information of all courses by taking input from the user.
- Fill the main Map by adding the course name as a key and user filled Map as a value.
- Within for loop, we first convert all the Map’s keys into an ArrayList of type Object.
- After that, we get the object from the list by using the get() method and get value from the Map corresponding to this object for printing value.
Let’s implement the logic for creating Map of Map to store data.
MapOfMapExample.java
Output:
Next TopicPrint Pencil Shape Pattern in Java