如何解决“类型List <map.entry <string,string >>的方法values()未定义”

时间:2019-01-14 00:05:50

标签: java list arraylist

首先,我有两个字符串arraylist。然后,我将它们与这两个arraylist字符串组合在一起。

两个字符串arraylist看起来像这样:

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)));
            }
        }

,将两个字符串arraylist合并后的结果如下:

[a1=z1, a2=z2,a3=z3,a3=z3]

现在我像这样删除重复项:

multiall = multiall.stream().distinct().collect(Collectors.toList());

其结果如下:

[a1=z1, a2=z2,a3=z3]

现在我要做的是将其转换为字符串arraylist。我已经尝试过:

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]作为字符串数组列表。那可能吗?还是我的概念错了?

请帮助我。谢谢

1 个答案:

答案 0 :(得分:2)

.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);