Java:如何使用Google的HashBiMap?

时间:2023-02-06 20:47:59

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的类型为 ,而V的类型为 >。

    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。

2 个解决方案

#1


15  

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 BiMap documentation (HashBiMap impelements BiMap):

但它看起来仍然很奇怪。我想你应该使用另一个系列。来自BiMap文档(HashBiMap inslements 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(或“双向映射”)是一种保留其值及其键值的唯一性的映射。此约束使bimaps支持“反向视图”,这是另一个包含与此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)两次,则multimap包含从键到两个值的映射。

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);
}

#2


8  

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

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");

#1


15  

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 BiMap documentation (HashBiMap impelements BiMap):

但它看起来仍然很奇怪。我想你应该使用另一个系列。来自BiMap文档(HashBiMap inslements 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(或“双向映射”)是一种保留其值及其键值的唯一性的映射。此约束使bimaps支持“反向视图”,这是另一个包含与此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)两次,则multimap包含从键到两个值的映射。

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);
}

#2


8  

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

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");