英文:
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<NameValuePair>
. To do certain checks and verifications on those params, I need to convert that list into a 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
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.
答案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<Map.Entry<String, String>> entries = list.stream()
.map(n->new AbstractMap.SimpleEntry<String, String>(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<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";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论