问题描述
我正在使用 com.apache.http.NameValuePair
从请求URL捕获参数,这些参数基本上将这些参数存储在 List< NameValuePair>
中.为了对这些参数进行某些检查和验证,我需要将该列表转换为 HashMap< String,String>
.有没有办法进行这种转换?
I'm capturing parameters from a request url using com.apache.http.NameValuePair
which 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< NameValuePair>进入hashMap< String,String> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!