Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Add the following code to the buildMovieMap0 method: Map topMovies = new HashMap(); topMovies.put( 1 , The Shawshank Redemption); topMovies.put(2, The Godfather); topMovies.put(3, The

image text in transcribedimage text in transcribed

1. Add the following code to the buildMovieMap0 method: Map topMovies = new HashMap(); topMovies.put( 1 , "The Shawshank Redemption"); topMovies.put(2, "The Godfather"); topMovies.put(3, "The Dark Knight"); topMovies.put( 4, "The Godfather: Part II"); topMovies.put(5, "12 Angry Men"); System.out.println(topMovies.get(3)); We created a Map named topMovies and added 5 entries. Note that the Map works with keys of the Integer type and values of the String type. We also retrieved the value associated with the key 3 and printed it out to the console. Run the program to see the output. 2. Next, add the following code to the buildMovieMap0 method below what you have so far: System. out.println(topMovies. keySet( )); System. out. println(topMovies.values( )); System. out. println(topMovies. entrySet()) Run the program and look at the output. The first line of output prints just the keys, the second line prints just the values, and the third line prints the key-value pairs. 3. Now it's your turn. Create a HashMap inside the buildEmployeeMap0 method that takes in String keys and Integer values, then name it employees. This map will contain entries of employee names and employee IDs. 4. Add the following employees to the map: - Deer Park, 345 - Homer Simpson , 234 - Gordon Freeman, 445 Then have the method return your employees HashMap instead of null. 5. Add the following code to the run 0 method below what is there so far: This code calls the buildEmployeeMap0 method and returns a HashMap. It then assigns that HashMap to the employees variable. Next it calls the other methods in the class and passes that employee Map to those methods as arguments. This will allow you to use the employee HashMap in the other methods in this class. 6. Inside of the getEmployeeID0 method, call the get0 method on the employee Map, using the key Gordon Freeman. Then return the result. 7. Inside the getKeySet0 method, return the result of calling the keySet0 method on employees. 8. Inside the getvalues 0 method, return the result of calling the values 0 method on employees. 9. Inside the getEntrySet0 method, return the result of calling the entrySet0 method on employees. Run the program to see the results. Sample output: The Dark Knight [1,2,3,4,5] [The Shawshank Redemption, The Godfather, The Dark Knight, The Godfather: Part II, 12 Angry Men ] [1=The Shawshank Redemption, 2=The Godfather, 3=The Dark Knight, 4=The Godfather: Part II, 5=12 Angry Men] The employees ID is: 445 employees keySet: [Gordon Freeman, Deer Park, Nikola Tesla, Homer Simpson] employees values: : [445, 345, 543, 234] employees entrySet: [Gordon Freeman=445, Deer Park=345, Nikola Tesla=543, Homer Simpson=234]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions