Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To instantiate BiMap Of google-collections in Java

Tags:

java

guava

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

like image 395
Léo Léopold Hertz 준영 Avatar asked Mar 11 '10 21:03

Léo Léopold Hertz 준영


2 Answers

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(); 
like image 178
Michael Myers Avatar answered Nov 04 '22 03:11

Michael Myers


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.

like image 44
brabster Avatar answered Nov 04 '22 01:11

brabster