Hot questions for Operating HashBiMap in Guava

Top 10 Java Open Source / Guava / Operating HashBiMap

Java: how to use Google's HashBiMap?

Question: 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>>.

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

Answer: 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): 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.

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: 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.

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