繁体   English   中英

如何从(Google Guava)Multimap 打印键的匹配值?

[英]How to print the matching value of key from (Google Guava) Multimap?

我想从 multimap 打印键的匹配值。 到目前为止,我一直没有成功。

这些是我到目前为止得到的代码行:

 dictionaryGG.keys().forEach((key) -> {
            if (userInput.equals(key)) {
                System.out.println("User Input equal to KEY");
                System.out.println("Value associated with matching KEY" + value); // get an error here "value cannot be resolved to a variable"
            }
            System.out.println(key);
        }); ```

(dictionaryGG is my multimap)

请阅读Multimap上的 Guava Wiki 页面

有两种方法可以从概念上考虑 Multimap:作为从单个键到单个值的映射的集合:

 a -> 1 a -> 2 a -> 4 b -> 3 c -> 5

或者作为从唯一键到 collections 个值的映射:

 a -> [1, 2, 4] b -> [3] c -> [5]

也就是说,您可以通过简单地使用Multimap#get(K)获取映射到多映射键的值:

Collection<String> values = dictionaryGG.get(userInput);
System.out.println("Values: " + values); // assuming you store strings

请注意,如果您不希望每个键有多个值,一个简单的Map & Map#get(Object)就足够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM