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.