只是为了练习,我想使用 Java 8 的 stream 功能来实现代码,我在其中寻找一个 HTTP header 名称并返回与之关联的值.
例如,只要说 activeResponse 是我从 url 呈现的 HttpResponse。
而不是做: Iterating through headers with a for-loop
我想使用流来实现这段代码。知道怎么做吗?
最佳答案
String result = Arrays.stream(headers)
.filter(h -> h.getName().equals("Content-Type"))
.findFirst()
.map(Header::getValue)
.orElse("NotFound");
关于http - 在 HTTP header 上使用 java 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44578669/