12/13 restaurantMenu.get()


#1

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");

		System.out.println(restaurantMenu.size());
    System.out.println("");
    System.out.println(restaurantMenu.keySet());
    System.out.println("");

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

		}


Hope this makes sense!


#2

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.


#3

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