To instantiate BiMap Of google-collections in Java
Asked Answered
K

3

16

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>.

Klaipeda answered 11/3, 2010 at 21:49 Comment(0)
Z
35

As stated in the linked question, you are supposed to use the create() factory methods.

In your case, this means changing

this.wordToWordID = new BiMap<String. Integer>();

to

this.wordToWordID = HashBiMap.create(); 
Zach answered 11/3, 2010 at 21:52 Comment(4)
Hmm. Your answer raises a new question. Why does EnumBimap not have the method create without parameters, like HashBiMap?Irresponsible
@Masi: That's a good question. I believe the reason is because EnumBimap needs to know what its parameters are, and because of type erasure it can't know unless you pass the Class objects to it at some point. The same is true of EnumMap and EnumSet in the standard library.Zach
So it is not enough for EnumMap to know the types only. It apparently makes some processing based on the content of the input data.Irresponsible
@Masi: Yes, if you look at the source you can see that EnumMap uses the Class object to figure out what all the possible values of the enum are (and for other things; you can see the OpenJDK source at openjdk.dev.java.net/source/browse/openjdk/jdk/trunk/jdk/src/…). EnumBimap requires the Class arguments simply because it uses two EnumMap instances (see the source here: code.google.com/p/google-collections/source/browse/trunk/src/…).Zach
S
6

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.

Scrimpy answered 11/3, 2010 at 21:53 Comment(1)
URL's no more up to dateOutpouring
E
6

Another cool way to create a BiMap, but in this case an immutable BiMap, is using the 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

Eglanteen answered 21/3, 2015 at 12:12 Comment(2)
I think this is even better than Michael's answer in some cases. Immutable for efficiency, not having nulls. What do you think? I think this feature appeared after I asked the question. Manual about it here docs.guava-libraries.googlecode.com/git/javadoc/com/google/…Irresponsible
Props for the javadocs @Masi, it didn't occur to me to put them here as well. I guess that Immutability vs. Mutability is a different discussion, but as I always use immutable objects I just thought that at least this way was worth a mention.Eglanteen

© 2022 - 2024 — McMap. All rights reserved.