12/13 General knowledge question Important!


I have a couple of question that came up during the exercise 12/13

1st of all : In the first example : ''' for (String name: myFriends.keySet()) {

System.out.println(name + " is age: " + myFriends.get(name));'''

and the exercise : for (String item : restaurantMenu.keySet()) {

How is the software able to identify if it's a name or an item ? it's really strange that it can know that mark is a name and pizza is an item when we never entered anything about it . Does the software contain all the name in the world ? and all items ?
I hope i don't sound stupid after this lol but it's strange

And what is .keySet in the above example ?

I would highly apreciate ifsomeone could explain this to me. Because i'm eager to learn everything .
and btw this is the best website out there


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


		for (String item : restaurantMenu.keySet()) {

			System.out.println("A " + item + " costs " + restaurantMenu.get(item) + " dollars.");




No,software doesn't know what is name and what is item :slight_smile:

you entered something here

for (String item : restaurantMenu.keySet()) {

you took a varible named item .you can take any name here.
for example you can take xyz
then your for loop should be like

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


thats why i said,software doesn't know what is name and what is item.

Returns a Set view of the keys contained in the map.
run this in your code

for (String item : restaurantMenu.keySet()) {
//System.out.println("A " + item + " costs " + restaurantMenu.values() + " dollars.");


it'll make sense :slight_smile:

hope you've understand now.feel free to ask any question :slight_smile:


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