如何转换List< NameValuePair>进入hashMap< String,String&gt ;? [英] How to convert List<NameValuePair> into a hashMap<String, String>?

查看:126
本文介绍了如何转换List< NameValuePair>进入hashMap< String,String&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 com.apache.http.NameValuePair 从请求URL捕获参数,这些参数基本上将这些参数存储在 List< NameValuePair> 中.为了对这些参数进行某些检查和验证,我需要将该列表转换为 HashMap< String,String> .有没有办法进行这种转换?

I'm capturing parameters from a request url using com.apache.http.NameValuePairwhich basically store those params in List<NameValuePair>. To do certain checks and verifications on those params, I need to convert that list into a HashMap<String, String>. Is there a way to do this conversion?

推荐答案

您使用Java 8吗?在这种情况下,您可以使用Collectors.toMap()方法:

Do you use Java 8? In that case you could make use of the Collectors.toMap() method:

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

否则,您将不得不遍历所有元素

Otherwise you would have to loop through the elements

for(NameValuePair element : list) {
  //logic to convert list entries to hash map entries
}

为获得更好的理解,请查看以下教程.

To get a better understanding, please take a look at this tutorial.

这篇关于如何转换List&lt; NameValuePair&gt;进入hashMap&lt; String,String&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆