我有以下方法:

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,然后使用它。

关于java - 如何使用Guava的MultiMap输出唯一键? (Java),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31052780/

10-11 16:59