12/13 restaurantMenu.get()

Does restaurantMenu.get() return an integer value or just the second item declared in the HashMap?

I tried testing this to figure it out myself and its seems more likely to return the second item in the HashMap

I switched things around as seen below, starting the HashMap with an Integer first and then the String. Then when calling restuarantMenu.get() it returns the String Values instead of the Integer.

This question arose when I could not figure out how the for loop differentiated between ‘item’ and restaurant.get(item).

		HashMap<Integer, String> restaurantMenu = new HashMap<Integer, String>();

		restaurantMenu.put(13, "Turkey Burger");
		restaurantMenu.put(10, "Naan Pizza");
		restaurantMenu.put(11, "Cranberry Kale Salad");


		for (Integer item : restaurantMenu.keySet()) 
      System.out.println("A " + restaurantMenu.get(item) + " costs " + item + " 													dollars.");


Hope this makes sense!

item above is a key, and we can see that the keys of the HashMap are in this case, integer. Given a key, the value will be returned.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.