簡體   English   中英

如何使用Guava的MultiMap輸出唯一鍵? (JAVA)

[英]How to output unique keys with MultiMap from Guava? (Java)

我有以下方法:

public static void showStuff(Multimap<String, String> map) {
        for (String key : map.keys()) {
            System.out.println("The key, " + key
                    + ", has the results: " + map.get(key));
        }
    }

這輸出:

The key, **one**, has the results: [a,b,c,d,d,e]
The key, **one**, has the results: [a,b,c,d,d,e]
The key, **one**, has the results: [a,b,c,d,d,e]
The key, **one**, has the results: [a,b,c,d,d,e]
The key, **one**, has the results: [a,b,c,d,d,e]
The key, **two**, has the results: [a,a,a,b,b,e]
The key, **two**, has the results: [a,a,a,b,b,e]

我怎么才能輸出唯一的密鑰。 我希望它只打印一次語句而不是多次重復它。 我有一個表,其中不同的行具有重復鍵,因此我使用MultiMap獲取重復鍵的所有值。 我現在如何僅輸出

The key, **one**, has the results: [a,b,c,d,d,e]
The key, **two**, has the results: [a,a,a,b,b,e]

謝謝!

您可以使用Multimap.keySet()來獲取不同的鍵。

您可以使用Multimap.entries()來獲取(鍵,值)對,而不必返回到地圖以請求與每個鍵關聯的值。

或者,您可以使用Multimap.asMap()將其轉換為java.util.Map ,然后使用它。

使用map.keySet()而不是map.keys()

暫無
暫無

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

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