簡體   English   中英

如何解決“List 類型的方法 values() 未定義” <Map.Entry<String,String> &gt;”

[英]How to solve "The method values() is undefined for the type List<Map.Entry<String,String>>"

首先,我有兩個字符串數組列表。 然后我將這兩個數組列表字符串組合在一起。

兩個字符串數組列表如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM