java - 如何转换列表进入列表>?
问题描述
我正在从请求 url 中捕获参数,使用com.apache.http.NameValuePair
它基本上将这些参数存储在List<NameValuePair>
. 要对这些参数进行某些检查和验证,我需要将该列表转换为List<Map.Entry<String, String>>
. 有没有办法进行这种转换?
像这样的东西:
http://127.0.0.1:9898/v3/ {project_id}/eip/publicips?fields=id&fields=owner
从如何将 List<NameValuePair> 转换为 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());
推荐阅读
- reactjs - 在 Redux 中操作后更改键
- node.js - 用于上传视频的 Vimeo api 节点返回错误“源对象可能只是此环境中 Buffer 或 Readable 的实例”
- java - X x = new X(); 之间的差异 和 x = 新 X();
- ios - 在不使用 init 的情况下模拟 URLSessionDataTask
- haskell - Haskell 中的种类以何种方式关联?
- reactjs - 如何使用 axios 拦截器自动刷新令牌
- c++ - C++ 中的初始化
- java - 无法使用 super 调用父接口的默认方法
- css - 如何在引导程序中拥有 100% 的剩余空间?
- c++ - 保存格式化文本 Qt 的文件