在 Java 中实例化 BiMap Of google-collections
如何实例化 Google 集合的 Bimap
?
我已阅读问题 Java:实例化 Google Collection 的 HashBiMap
我的代码示例
import com.google.common.collect.BiMap;
public class UserSettings {
private Map<String, Integer> wordToWordID;
UserSettings() {
this.wordToWordID = new BiMap<String. Integer>();
我得到无法实例化类型 BiMap
。
How can you instantiate a Bimap
of Google-collections?
I've read the question Java: Instantiate Google Collection's HashBiMap
A sample of my code
import com.google.common.collect.BiMap;
public class UserSettings {
private Map<String, Integer> wordToWordID;
UserSettings() {
this.wordToWordID = new BiMap<String. Integer>();
I get cannot instantiate the type BiMap<String, Integer>
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建 BiMap(但在本例中是不可变 BiMap)的另一种很酷的方法是使用
ImmutableBiMap.Builder
。http://docs.guava-libraries .googlecode.com/git/javadoc/com/google/common/collect/ImmutableBiMap.html
Another cool way to create a BiMap, but in this case an immutable BiMap, is using the
ImmutableBiMap.Builder
.http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ImmutableBiMap.html
正如链接问题中所述,您应该使用
create()
工厂方法。就您而言,这意味着更改
为
As stated in the linked question, you are supposed to use the
create()
factory methods.In your case, this means changing
to
BiMap 是一个接口,因此无法实例化。您需要根据所需的属性实例化一个具体的子类,可用的子类(根据javadoc)为EnumBiMap、EnumHashBiMap,HashBiMap,不可变BiMap。
BiMap is an interface, and as such cannot be instantiated. You need to instantiate a concrete subclass according to the properties you want, available subclasses (according to the javadoc) are EnumBiMap, EnumHashBiMap, HashBiMap, ImmutableBiMap.