我正在使用com.apache.http.NameValuePair
从请求URL中捕获参数,这些参数基本上将这些参数存储在List<NameValuePair>
中。为了对这些参数进行某些检查和验证,我需要将该列表转换为List<Map.Entry<String, String>>
。有没有办法进行这种转换?
类似这样:
http://127.0.0.1:9898/v3/ {project_id} / eip / publicips?fields = id&fields = owner
来自How to convert List<NameValuePair> into a hashMap<String, String>?
Map<String, String> mapped = list.stream().collect(
Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));
这对我不起作用。因为有很多字段键。
您可以将每个NameValuePair转换为Map.Entry,然后将它们收集到List
List<Map.Entry<String, String>> entries = list.stream()
.map(n->new AbstractMap.SimpleEntry<String, String>(n.getName(), n.getValue())
.collect(Collectors.toList());