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 Java Data Structures Efficiency! Design the UI

How does the for in promptAction() works

Can someone please explain me how this for works?

  private String promptAction() throws IOException {
    System.out.printf("There are %d songs available. Your options are: %n", 

    for (Map.Entry<String, String> option : menu.entrySet()) {
      System.out.printf("%s - %s %n",
    System.out.print("What do you want to do: ");
    String choice = reader.readLine();
    return choice.trim().toLowerCase();

1 Answer

Rafał Sobczyk
Rafał Sobczyk
3,274 Points

It iterates over every entry in out map, making it the value of variable "option" so we can use it later. If you don't know how the foreach loop - "for (Object o : objects)" works, watch the Feeling Loopy workshop here on TeamTreehouse. Basically, for every menu item in our menu map it prints the key and the value of that item.