Working with HashMap
Create a HashMap
to hold a company's directory of employees. That is a directory of Integer
ids that point to String
names in a lookup table. You will add a new employee as well as remove an employee from the directory.
This exercise is part of the course
Data Types and Exceptions in Java
Exercise instructions
- Import
HashMap
for use in the application. - Set the variable
directory
to a new instance ofHashMap
ofInteger
s andString
s. - Add a new employee (name of
"Marcia"
and id of31
) to thedirectory
map. - Remove
"Davy"
with id of45
from thedirectory
map.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
// Import HashMap
____ ____.____.____;
public class EmployeeDirectory {
public static void main(String[] args) {
// Create a HashMap directory of Integer to String using parameterized constructor
_____<_____, ____> directory = ____ ____<____, ____>();
directory.put(23, "Joye");
// Add employee "Marcia" with id 31 to the directory
directory.____(____, "____");
directory.put(45, "Davy");
System.out.println(directory);
String name = directory.get(31);
System.out.println(name);
// Remove "Davy" from the directory
directory.____(____);
System.out.println(directory);
}
}