I'm trying to generate custom HttpHeader in akka. I've a HashMap of header entries which I need to convert to Iterable. Here's my code:
import akka.http.javadsl.model.HttpHeader;
HashMap<String, String> headersMap = new HashMap<>();
headersMap.put("key1","value1");
return (Iterable<HttpHeader>) headersMap;
//HttpRequest.create().withUri("uri").addHeaders(Iterable<HttpHeader> iterable)
public static Iterable<HttpHeader> convertToRecordHttpHeaders(Map<String, String> headersMap){
return headersMap.entrySet().stream()
.map(x -> new HttpHeader(x.getKey(), x.getValue()))
.collect(Collectors.toList());
}
What's the efficient way to convert the map to HttpHeader and convert it to Iterable as I want to create a HttpRequest in akka.
CodePudding user response:
A HashMap
is not an Iterable
anything. Its entrySet()
, keySet()
and values()
are, but for your map those are Iterable<Map.Entry<String, String>>
, Iterable<String>
and Iterable<String>
respectively. That means that you cannot cast the map in any way to an Iterable<HttpHeader>
.
What you can do is convert:
List<HttpHeader> headersList = headersMap.entrySet().stream()
.map(entry -> new HttpHeader(entry.getKey(), entry.getValue())
.collect(Collectors.toList()); // or just toList() with Java 17
This assumes that HttpHeader
has a constructor that takes a header name and value. If it doesn't, replace the constructor call with something else that creates an HttpHeader
.
CodePudding user response:
Lets say your Header class is something like this
class HttpHeader{
public HttpHeader(Map.Entry<String,String> header){}
}
then just use constructor reference in place of lambda or else you can provide your parsing using lambda expression
headersMap.entrySet().stream().map(HttpHeader::new).iterator();
But still i don't understand the use case of creating HashMap for return HttpHeader iterator.
i guess you used to keep header keys unique viva map however you can use TreeSet that will maintain unique objects too
class HttpHeader implements Comparable<HttpHeader>{
String key;
String value;
public HttpHeader(String key, String value){
this.key = key;
this.value = value;
}
@Override
public int compareTo(HttpHeader o) {
return key.compareTo(o.key);
}
}