javaloops

Can't iterate through HttpHeaders Map


My IDE says

Error: can only iterate over an array or an instance of java.lang.Iterable

On the headers.map() in this code

import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;

public static String dumpRequest(HttpRequest request)
{
    HttpHeaders headers = request.headers();
    for (Map.Entry<String, List<String>> header : headers.map()) {

    }
}

However the documentation of the map() in my IDE says it returns a Map<String, List<String>> , what am I missing ? please consider I'm very new to JAVA

I'm using JAVA 14

This is the documentation of HttpHeaders that I'm using


Solution

  • Close.

    for (Map.Entry<String, List<String>> header : headers.map().entrySet())
    

    Map itself does not extend the Iterable interface, so it can't be iterated using foreach.