如何将List 转换为List >?

问题描述 投票:0回答:1

我正在使用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));

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

java list java-8
1个回答
0
投票

您可以将每个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());
© www.soinside.com 2019 - 2024. All rights reserved.