首先,我有两个字符串数组列表。然后我将它们组合在一起,将这两个数组列表字符串组合在一起。
两个字符串数组列表如下所示:
d1 = [a1,a2,a3,a3]
d2 = [z1,z2,z3,z3]
正如我从人们的建议中发现的那样,我将这两个字符串数组列表组合在一起,如下所示:
List<Map.Entry<String, String>> multiall = new ArrayList<>(d1.size());
if (d1.size() == d2.size()) {
for (int i = 0; i < d1.size(); ++i) {
multiall.add(new AbstractMap.SimpleEntry<String, String>(d1.get(i), d2.get(i)));
}
}
组合两个字符串数组列表后的结果如下所示:
[a1=z1, a2=z2,a3=z3,a3=z3]
现在我像这样删除了重复项:
multiall = multiall.stream().distinct().collect(Collectors.toList());
其结果是这样的:
[a1=z1, a2=z2,a3=z3]
现在我想做的是我想将其转换为字符串数组列表。我试过这样:
ArrayList<String> targetList = new ArrayList<>(multiall.values());
但是我这样说时出错:
The method values() is undefined for the type List<Map.Entry<String,String>>
我的预期输出是这样的:
[a1=z1,a2=z2,a3=z3]
作为字符串数组列表。 这可能吗? 还是我的概念是错误的?
请帮帮我。谢谢
.values()
使用地图,而不是列表。
尝试:
List<String> targetList = multiall.stream()
.map(Map.Entry::getValue)
.collect(Collectors.toList());
此外,List<Map.Entry<String, String>>
看起来不对。您应该改用Map
。
更新
将流映射更改为:-
.map(entry -> entry.getKey() + "=" + entry.getValue())
您还可以将List<Map.Entry>
更改为地图并使用覆盖的toString()
:-
Map<String, String> stringMap = multiall.stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.println(stringMap);