Java:如何使用 Google 的 HashBiMap?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2574685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:51:11  来源:igfitidea点击:

Java: how to use Google's HashBiMap?

javaguava

提问by hhh

Keys are a file and a word. The file gives all words inside the file. The word gives all files having the word. I am unsure of the domain and co-domain parts. I want K to be of the type <String>and V to be of type <HashSet<FileObject>>.

密钥是一个文件和一个词。该文件给出了文件中的所有单词。这个词给出了所有有这个词的文件。我不确定域和共同域部分。我希望 K 属于类型,<String>而 V 属于类型<HashSet<FileObject>>

    public HashBiMap<K<String>,V<HashSet<FileObject>>> wordToFiles 
            = new HashBiMap<K<String>,V<HashSet<FileObject>>>();

    public HashBiMap<K<String>,V<HashSet<FileObject>>> fileToWords 
            = new HashBiMap<K<String>,V<HashSet<FileObject>>>();

Google's HashBiMap.

谷歌的 HashBiMap。

回答by Roman

change it to

将其更改为

public HashBiMap<String,HashSet<FileObject>> wordToFiles = HashBiMap.create ();

But still it looks very strange. I think you should use another collection. From BiMapdocumentation (HashBiMapimpelements BiMap):

但它看起来仍然很奇怪。我认为你应该使用另一个集合。从BiMap文档(HashBiMapimpelements BiMap):

A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.

双映射(或“双向映射”)是一种保留其值及其键的唯一性的映射。此约束使 bimap 能够支持“反向视图”,这是另一个 bimap,包含与该 bimap 相同的条目,但具有相反的键和值。

I don't know the problem you want to solve but after looking at your code I can suggest to consider using Multimaps. From its docs:

我不知道您要解决的问题,但在查看您的代码后,我可以建议考虑使用 Multimaps。从它的文档:

A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.

类似于 Map 的集合,但可以将多个值与单个键相关联。如果使用相同的键但不同的值调用 put(K, V) 两次,则多映射包含从键到两个值的映射。

For example, you can do something like this:

例如,您可以执行以下操作:

Multimap<String, FileObject> wordToFiles = HashMultimap.create();
wordToFiles.put("first", somefile);
wordToFiles.put("first", anotherfile);
for (FileObject file : wordToFiles.get("first"){
   doSomethingWithFile (file);
}

回答by Sibin John Mattappallil

Add this dependency to your 'build.gradle'

将此依赖项添加到您的“build.gradle”

compile 'com.google.guava:guava:19.0'

import BiMap and HashBiMap

导入 BiMap 和 HashBiMap

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

Create a bimap

创建双图

BiMap<String, String> myBiMap = HashBiMap.create();

Put some values

放一些值

myBiMap.put("key", "value");

Get mapping value by key,

按键获取映射值,

myBiMap.get("key");

Get mapping by value,

按值获取映射,

myBiMap.inverse().get("value");