Map of Map in Java
The map is now a Java interface for mapping keys to values. It is frequently necessary to use Map of Map (nested Map).
Nested Maps are useful in various situations, including keeping students' names together with respective course Ids. In this example, we build a Map with a key, i.e., course, and value, i.e., another Map with a key, i.e., Id, and value, i.e., the patient's name.
To make a Map of Map in Java, take these steps:
- For every course, sum the number of students from either the user.
- Fill in the student registration for all classes based on the user's input.
- Filling up the primary Map with the course name like a keyword and the user-created Map as a value.
- We begin inside the for a loop by converting all of the Map's values into an ArrayList of class Object.
- Following that, we use the get() function to retrieve the item in the list and the information from the Map relating to this item for displaying.
MapEx.java
// this program is for map of map in Java
//importing the required packages
import java.util.HashMap;
import java.util.Map;
import java.util.*;
//To make a MapOfMap Sample, define the class MapOfMapEx.
public class MapEx {
// main section of the program
public static void main(String[] args) {
// the variables are then declared
int sizes1 = 0;
int sizes2 = 0;
// develop a map of all BC students which should contain their names and IDs.
Map<Object,String> bcStudents = new HashMap<Object, String>();
// design a map for MC students that will contain their names and IDs.
Map<Object,String> mcStudents = new HashMap<Object, String>();
// an object is created for the scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the students in Bc.");
sizes1 = Integer.parseInt(sc.nextLine());
System.out.println("Please enter the students in Mc.");
sizes2 = Integer.parseInt(sc.nextLine());
// Fill up the bcStudents Map using user-provided data.
for(int i = 1; i <= sizes1; i++){
int ids= 100;
String names = "";
System.out.println("Please enter the name of the " + i + "st student in BC:");
names = sc.nextLine();
System.out.println("The Id of the Student is: = " + (ids+i) + " Students Names = "+names);
ids = ids + i;
bcStudents.put(Integer.valueOf(ids), names);
}
// fill mcaStudents Map by taking input from the user
for(int i = 1; i <= sizes2; i++){
int ids= 100;
String names = "";
System.out.println("Please enter the name of the " + i + "st student of MCA:");
names = sc.nextLine();
System.out.println("Student Id = " + (ids+i) + " Student Name = "+names);
ids = ids + i;
mcStudents.put(Integer.valueOf(ids), names);
}
// build a map including information on BC and MC students.
Map<String, Object> students1 = new HashMap<String, Object>();
students1.put("BC", bcStudents);
students1.put("MC", mcStudents);
// the mapping of the student is printed
System.out.println("The Map of Map is : " + students1);
// the student's names is printed
for (int i = 0; i < students1.size(); i++){
ArrayList<Object> datas = new ArrayList<Object>(students1.keySet());
Object object = datas.get(i);
System.out.println("Courses: " + object + " Student: " + students1.get(object));
}
// close Scanner class object
sc.close();
}
}
Output
Please enter the students in Bc.
3
Please enter the students in Mc.
3
Please enter the name of the 1st student in BC:
Sai
The Id of the Student is: = 101 Students Names = Sai
Please enter the name of the 2st student in BC:
Ram
The Id of the Student is: = 102 Students Names = Ram
Please enter the name of the 3st student in BC:
Raghu
The Id of the Student is: = 103 Students Names = Raghu
Please enter the name of the 1st student of MCA:
Anusha
Student Id = 101 Student Name = Anusha
Please enter the name of the 2st student of MCA:
Sita
Student Id = 102 Student Name = Sita
Please enter the name of the 3st student of MCA:
Latha
Student Id = 103 Student Name = Latha
The Map of Map is : {BC={101=Sai, 102=Ram, 103=Raghu}, MC={101=Anusha, 102=Sita, 103=Latha}}
Courses: BC Student: {101=Sai, 102=Ram, 103=Raghu}
Courses: MC Student: {101=Anusha, 102=Sita, 103=Latha}