如何将 List 转换为 List>?

huangapple go评论76阅读模式
英文:

How to convert List<NameValuePair> into a List<Map.Entry<String, String>>?

问题

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

来自https://stackoverflow.com/questions/35947858/how-to-convert-listnamevaluepair-into-a-hashmapstring-string

Map<String, String> mapped = list.stream().collect(
        Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));

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

英文:

I'm capturing parameters from a request url using com.apache.http.NameValuePair which basically store those params in a List&lt;NameValuePair&gt;. To do certain checks and verifications on those params, I need to convert that list into a List&lt;Map.Entry&lt;String, String&gt;&gt;. Is there a way to do this conversion?

something like this:

http://127.0.0.1:9898/v3/{project_id}/eip/publicips?fields=id&fields=owner

from https://stackoverflow.com/questions/35947858/how-to-convert-listnamevaluepair-into-a-hashmapstring-string

Map&lt;String, String&gt; mapped = list.stream().collect(
        Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));

It is not work for me. because there are many fields key.

答案1

得分: 3

你可以将每个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());
英文:

You can convert each NameValuePair into Map.Entry and then collect them into List

List&lt;Map.Entry&lt;String, String&gt;&gt; entries = list.stream()
                                              .map(n-&gt;new AbstractMap.SimpleEntry&lt;String, String&gt;(n.getName(), n.getValue())
                                              .collect(Collectors.toList());

答案2

得分: 0

可能你已经搞乱了代码片段之外的其他部分。我尝试复现了你的情况,在我的设置中是可以工作的。

public class Main {

    public static void main(String[] args) {
        List<NameValuePair> list = new ArrayList<>();
        MyNameValuePair myNameValuePair = new MyNameValuePair();
        myNameValuePair.setKvKey("i");
        list.add(myNameValuePair);
        myNameValuePair = new MyNameValuePair();
        myNameValuePair.setKvKey("j");
        list.add(myNameValuePair);
        myNameValuePair = new MyNameValuePair();
        myNameValuePair.setKvKey("k");
        list.add(myNameValuePair);
        Map<String, String> mapped = new HashMap<>();
        mapped = list.stream().collect(
                Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));

        System.out.println(mapped);
    }
}

class MyNameValuePair implements NameValuePair {
    private String kvKey;
    public void setKvKey(String kvKey){
        this.kvKey = kvKey;
    }

    @Override
    public String getName() {
        return kvKey;
    }

    @Override
    public String getValue() {
        return "val";
    }
}
英文:

Probably you've messed something else that's not included in the code excerpt. I've tried to reproduce your case and it works in my setup.


public class Main {

    public static void main(String[] args) {
        List&lt;NameValuePair&gt; list = new ArrayList&lt;&gt;();
        MyNameValuePair myNameValuePair = new MyNameValuePair();
        myNameValuePair.setKvKey(&quot;i&quot;);
        list.add(myNameValuePair);
        myNameValuePair = new MyNameValuePair();
        myNameValuePair.setKvKey(&quot;j&quot;);
        list.add(myNameValuePair);
        myNameValuePair = new MyNameValuePair();
        myNameValuePair.setKvKey(&quot;k&quot;);
        list.add(myNameValuePair);
        Map&lt;String, String&gt; mapped = new HashMap&lt;&gt;();
        mapped = list.stream().collect(
                Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));

        System.out.println(mapped);
    }
}

class MyNameValuePair implements NameValuePair {
    private String kvKey;
    public void setKvKey(String kvKey){
        this.kvKey = kvKey;
    }

    @Override
    public String getName() {
        return kvKey;
    }

    @Override
    public String getValue() {
        return &quot;val&quot;;
    }
}

huangapple
  • 本文由 发表于 2020年5月29日 11:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/62078270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定