Java:如何使用Google的HashBiMap? [英] Java: how to use Google's HashBiMap?

查看:155
本文介绍了Java:如何使用Google的HashBiMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

键是一个文件和一个单词.该文件给出了文件中的所有单词.单词给出了所有带有单词的文件.我不确定域和共域部分.我希望K的类型为<String>,V的类型为<HashSet<FileObject>>.

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

Google的HashBiMap.

推荐答案

将其更改为

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

但是看起来还是很奇怪.我认为您应该使用另一个集合.从BiMap文档(HashBiMap要素BiMap):

But still it looks very strange. I think you should use another collection. From BiMap documentation (HashBiMap impelements BiMap):

双向图(或双向图")是 保留唯一性的地图 它的价值以及它的价值 键.此约束启用双图 支持反向视图",即 另一个包含相同图的双图 条目作为此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.

我不知道您要解决的问题,但是在查看您的代码后,我建议您考虑使用Multimaps.从其文档中:

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:

类似于地图的集合,但是 可能会关联多个值 用一个键.如果您调用put(K, V)两次,使用相同的键,但 不同的值,多​​图 包含从键到两者的映射 值.

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

这篇关于Java:如何使用Google的HashBiMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆