首先,我有两个字符串数组列表。然后我将这两个数组列表字符串组合在一起。
两个字符串数组列表如下所示:
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>>
看起来不太对劲。你应该使用 aMap
代替。
更新
将流图更改为:-
.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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句