192k views
0 votes
Create a map using the Java map collection. The map should have 4 entries representing students. Each entry should have a unique student ID for the key and a student name for the element value. The map content can be coded in directly, you do not have to allow a user to enter the map data. Your program will display both the key and the value of each element.

User Gege
by
5.0k points

1 Answer

3 votes

Answer:

In Java:

import java.util.*;

public class Main{

public static void main(String[] args) {

Map<String, String> students = new HashMap<>();

students.put("STUD1", "Student 1 Name");

students.put("STUD2", "Student 2 Name");

students.put("STUD3", "Student 3 Name");

students.put("STUD4", "Student 4 Name");

for(Map.Entry m:students.entrySet()){

System.out.println(m.getKey()+" - "+m.getValue()); }

}}

Step-by-step explanation:

This creates the map named students

Map<String, String> students = new HashMap<>();

The next four lines populates the map with the ID and name of the 4 students

students.put("STUD1", "Student 1 Name");

students.put("STUD2", "Student 2 Name");

students.put("STUD3", "Student 3 Name");

students.put("STUD4", "Student 4 Name");

This iterates through the map

for(Map.Entry m:students.entrySet()){

This prints the details of each student

System.out.println(m.getKey()+" - "+m.getValue()); }

User Ashkan Rahmani
by
4.6k points