Question: Hold the broccoli, again! Create a new Map with entries, remove one of its entries, then check if the value is still in the Map.

Hold the broccoli, again!
Create a new Map with entries, remove one of its entries, then check if the value is still in the Map.
Steps:
* Initialize a new TreeMap of type String called vegetables.
* Add the following key:value pairs to the Map:"c" : "carrot""b" : "broccoli""s" : "spinach""t" : "tomato""a" : "asparagus"
* Print the Map to the console using System.out.println("Before: "+ vegetables);.
* Remove "broccoli" from the Map by using its key.
* Print the Map to the console again using System.out.println("After: ",vegetables);.
* Declare a boolean variable named containsBroccoli and set it equal to a method that will check if "broccoli" is still in the Map vegetables.
* Execute your code.
* Does the Map contain "b" : "broccoli"? The result should be false.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class TreeMapQuestion {
public static void main(String[] args){
// WRITE YOUR CODE HERE
/***** DO NOT CHANGE THE CODE BELOW THIS LINE *****/
System.out.println();
System.out.println("Does the Map contain 'broccoli'? "+ containsBroccoli);
}// end of main()
}
EXPECTED SDOUT
Before:{a=asparagus,b=broccoli,c=carrot,s=spinach,t=tomato}
After:{a=asparagus,c=carrot,s=spinach,t=tomato}
DoestheMapcontain'broccoli'?false

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!