Java: how to use Google's HashBiMap?

前端 未结 2 377
有刺的猬
有刺的猬 2020-12-19 17:00

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

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 17:39

    change it to

    public HashBiMap> 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 wordToFiles = HashMultimap.create();
    wordToFiles.put("first", somefile);
    wordToFiles.put("first", anotherfile);
    for (FileObject file : wordToFiles.get("first"){
       doSomethingWithFile (file);
    }
    

提交回复
热议问题