CommencerCommencer gratuitement

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

Afficher le cours

Instructions

  • Import HashMap for use in the application.
  • Set the variable directory to a new instance of HashMap of Integers and Strings.
  • Add a new employee (name of "Marcia" and id of 31) to the directory map.
  • Remove "Davy" with id of 45 from the directory map.

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);
	}
}
Modifier et exécuter le code