题
你怎能实例 Bimap
谷歌-集合?
我已经阅读问题 Java:化谷歌收集的HashBiMap
一样的我的代码
import com.google.common.collect.BiMap;
public class UserSettings {
private Map<String, Integer> wordToWordID;
UserSettings() {
this.wordToWordID = new BiMap<String. Integer>();
我得到 cannot instantiate the type BiMap<String, Integer>
.
解决方案
正如在关联的问题,你应该使用 create()
工厂的方法。
在你的情况,这意味着改变
this.wordToWordID = new BiMap<String. Integer>();
要
this.wordToWordID = HashBiMap.create();
其他提示
BiMap 是一个接口,因此无法实例化。你需要的实例的具体类根据的属性,可用的亚类(按照如果是的话,为什么不试)是 EnumBiMap, EnumHashBiMap, HashBiMap, ImmutableBiMap.
另一个很酷的方式来创建一个BiMap,但在这种情况下,一个不可改变的BiMap,使用 ImmutableBiMap.Builder
.
static final ImmutableBiMap<String, Integer> WORD_TO_INT =
new ImmutableBiMap.Builder<String, Integer>()
.put("one", 1)
.put("two", 2)
.put("three", 3)
.build();
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ImmutableBiMap.html
不隶属于 StackOverflow