Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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