简体   繁体   English

如何转换列表<namevaluepair>进入列表<map.entry<string, string> &gt;? </map.entry<string,></namevaluepair>

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

I'm capturing parameters from a request url using com.apache.http.NameValuePair which basically store those params in a List<NameValuePair> . I'm capturing parameters from a request url using com.apache.http.NameValuePair which basically store those params in a List<NameValuePair> . To do certain checks and verifications on those params, I need to convert that list into a List<Map.Entry<String, String>> .要对这些参数进行某些检查和验证,我需要将该列表转换为List<Map.Entry<String, String>> 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 http://127.0.0.1:9898/v3/ {project_id}/eip/publicips?fields=id&fields=owner

from How to convert List<NameValuePair> into a hashMap<String, String>?如何将 List<NameValuePair> 转换为 hashMap<String, String>?

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

It is not work for me.这对我不起作用。 because there are many fields key.因为有很多字段键。

You can convert each NameValuePair into Map.Entry and then collect them into List您可以将每个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());

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<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";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换List <NameValuePair> 进入hashMap <String, String> ? - How to convert List<NameValuePair> into a hashMap<String, String>? 如何创建列表<map.entry<string, integer> > 出一个 Map<string, integer> ? </string,></map.entry<string,> - How do I create a List<Map.Entry<String, Integer>> out of a Map<String, Integer>? 转换地图 <String, String> 列出 <NameValuePair> -这是最有效的吗? - Convert Map<String, String> to List<NameValuePair> - is this the most efficient? 排序列表<map.entry<string, integer> &gt; 主要和次要方式? </map.entry<string,> - Sort List<Map.Entry<String, Integer>> in primary and secondary way? 将 Map.Entry 列表转换为 LinkedHashMap - Convert List of Map.Entry to LinkedHashMap 如何解决“List 类型的方法 values() 未定义” <Map.Entry<String,String> &gt;” - How to solve "The method values() is undefined for the type List<Map.Entry<String,String>>" 无法从 Map.Entry 转换<String,Integer>到字符串 - Cannot convert from Map.Entry<String,Integer> to String 如何转换集合<list<map.entry<long, long> >> 列表<list<long> > 使用 java 流</list<long></list<map.entry<long,> - How to convert Collection<List<Map.Entry<Long, Long>>> to List<List<Long>> using java streams 将字符串转换为列表<NameValuePair> - Cast String to List<NameValuePair> HashMap之间的区别 <String,String> 和清单 <NameValuePair> - Difference between HashMap<String,String> and List<NameValuePair>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM