Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Can someone explain what this does is doing exactly here

for (Map.Entry<String, String> option : mMenu.entrySet()) {
      System.out.printf("%s - %s %n",
                        option.getKey(),
                        option.getValue());

We have a map and its printing getKey() and getValue() of what of this ????????

mMenu is a map here:
mMenu.put("add", "Add a new song to the song book");
      mMenu.put("quit", "Give up.  Exit the program");

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Nirbhay Agarwal !

So each time you put something into a map, you are adding an entry. An entry consists of a key and a value. So that enhanced for loop is looping through each entry in the map. It can be read as for each entry in the map....

getKey is getting the key portion so "add" is a key and "Add a new song to the songbook" is the value.

Does that make sense?

Yup Craig its grt I completed the KaraokeMachine concept... Thanks