>?,java,list,java-8"/>

首页 > 解决方案 > 如何转换列表进入列表>?

问题描述

我正在从请求 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));

这对我不起作用。因为有很多字段键。

标签: javalistjava-8

解决方案


您可以将每个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());

推荐阅读