HashMap | SoloLearn: Learn to code for FREE!

+1

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?

6/8/2017 2:52:38 PM

Jasurbek Kalandarov

1 Answer

New 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 .