如何实例化Google-集合的Bimap ?
我读过Java:实例化Google的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>。
发布于 2010-03-11 21:52:44
正如链接问题中所述,您应该使用create()工厂方法。
在你的例子中,这意味着改变
this.wordToWordID = new BiMap<String. Integer>();至
this.wordToWordID = HashBiMap.create(); 发布于 2010-03-11 21:53:13
BiMap是一个接口,因此不能实例化。您需要根据需要的属性实例化一个具体的子类,可用的子类(根据javadoc)是EnumBiMap、EnumHashBiMap、HashBiMap、ImmutableBiMap。
发布于 2015-03-21 12:12:22
另一种创建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
https://stackoverflow.com/questions/2428955
复制相似问题