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.
Cet exercice fait partie du cours
Data Types and Exceptions in Java
Instructions
- Import
HashMapfor use in the application. - Set the variable
directoryto a new instance ofHashMapofIntegers andStrings. - Add a new employee (name of
"Marcia"and id of31) to thedirectorymap. - Remove
"Davy"with id of45from thedirectorymap.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
// Import HashMap
import java.____.____;
public class EmployeeDirectory {
public static void main(String[] args) {
// Create a HashMap directory of Integer to String using parameterized constructor
HashMap<_____, ____> directory = new ____<____, ____>();
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);
}
}