How to convert a Headers entrySet.forEach to a String Array?

Viewed 1069

I am working on an HttpServer using the com.sun package and need to store the headers from the HttpExchange into a String array but I'm not sure how to.

I get the headers using:

Headers requestHeaders = exchange.getRequestHeaders();

And then I'm able to output them to the console using this:

requestHeaders.entrySet().forEach(System.out::println);

But instead of printing to the console, what is the best way to save them to a String array?

1 Answers

To get the keys use :-

String[] keyArray = requestHeaders.entrySet().stream()
                                  .map(Entry::getKey)
                                  .toArray(String[]::new);

To get the values use :-

String[] valuesArray = requestHeaders.values().stream().toArray(String[]::new);
Related