HashMap | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

HashMap

import java.util.HashMap; public class RestaurantForEach { public static void main(String[] args) { HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>(); restaurantMenu.put("Turkey Burger", 13); restaurantMenu.put("Naan Pizza", 11); restaurantMenu.put("Cranberry Kale Salad", 10); System.out.println(restaurantMenu.size()); for (String item: restaurantMenu.keySet()) { System.out.println("A " + item + " costs " + restaurantMenu.get(item) + " dollars."); } } } I cannot understand why restaurantMenu.get(item) is returning interger value. In for loop "item" is assigned as String, isn't it? Can anyone help?

8th Jun 2017, 2:52 PM
Jasurbek Kalandarov
Jasurbek Kalandarov - avatar
1 Answer
0
dear in a hash map the get() function returns the value of the integer corresponding to the string (known as key) when a string is passed as argument to it .
9th Jun 2017, 11:18 AM
NIKHIL MURARKA