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

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

3 Answers3

34

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(); 
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • Hmm. Your answer raises a new question. **Why does EnumBimap not have the method `create` without parameters, like HashBiMap?** – Léo Léopold Hertz 준영 Mar 12 '10 at 12:16
  • @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. – Michael Myers Mar 12 '10 at 14:07
  • 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. – Léo Léopold Hertz 준영 Mar 12 '10 at 14:40
  • @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 https://openjdk.dev.java.net/source/browse/openjdk/jdk/trunk/jdk/src/share/classes/java/util/EnumMap.java?rev=257&view=markup). `EnumBimap` requires the `Class` arguments simply because it uses two `EnumMap` instances (see the source here: http://code.google.com/p/google-collections/source/browse/trunk/src/com/google/common/collect/EnumBiMap.java). – Michael Myers Mar 12 '10 at 15:46
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.

brabster
  • 42,504
  • 27
  • 146
  • 186
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

Joao Sousa
  • 4,055
  • 1
  • 26
  • 29
  • 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 http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ImmutableBiMap.html – Léo Léopold Hertz 준영 Mar 21 '15 at 12:22
  • 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. – Joao Sousa Mar 23 '15 at 12:42