I tried to find a way to convert Map<String,String> into String[] using Java 8 way.
A Map
{"A":"a", "B","b", "C", "c"}`
should be converted to
["A", "a", "B", "b", "C", "c"] or ["B", "b", "C", "c", "A", "a"] ...
The order of the pairs does not matter but key/value pairs must be together.
The old way to do this is obvious:
Map<String, String> headers = .......
String[] array = new String[headers.size() * 2];
int i = 0;
for(Map.Entry<String, String> entry : headers.entrySet()) {
array[i++] = entry.getKey();
array[i++] = entry.getValue();
}
entrySetheaders.forEach((k, v) -> { array[i++] = k; array[i++] = v; });EntryStream.of(headers).flatMapKeyValue(Stream::of).toArray(String[]::new)