Get startedGet started for free

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

View Course

Exercise 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.

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);
	}
}
Edit and Run Code