如何使用Guava的MultiMap输出唯一键?(Java)

3
我有以下方法:
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]

感谢您!
2个回答

9

你可以使用 Multimap.keySet() 来获取唯一的键。

你可以使用 Multimap.entries() 来获取 (键, 值) 对,而不必返回映射以请求与每个键关联的值。

或者,你可以使用 Multimap.asMap() 将其转换为 java.util.Map,然后使用该 Map 进行操作。


2

使用 map.keySet() 替代 map.keys()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接