如何转换 List<NameValuePair>成一个 hashMap<String, String>? [英] How to convert List<NameValuePair> into a hashMap<String, String>?
问题描述
我正在使用 com.apache.http.NameValuePair
从请求 url 中捕获参数,它基本上将这些参数存储在 List
中.要对这些参数进行某些检查和验证,我需要将该列表转换为 HashMap
.有没有办法进行这种转换?
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>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!