Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Instantiate Google Collection's HashBiMap

Tags:

java

guava

bimap

I'm using Eclipse, and I've added google-collect.1.0-rc2.jar as a referenced library. Yet somehow this still doesn't work:

import com.google.common.collect.HashBiMap;

public class Odp {        
    //...
    
    HashBiMap<Character, Integer> charOcc =
        HashBiMap<Character, Integer>.create();    
}

Eclipse gives the following errors:

Multiple markers at this line

  • HashBiMap cannot be resolved
  • Character.Integer cannot be resolved
  • Syntax error on token ",", "." expected
  • Syntax error on token ".", delete this token
  • The method create() is undefined for class Odp

What am I doing wrong?

Other Google stuff, like Joiner, works fine. (But Joiner is not generic.)

like image 932
Nick Heiner Avatar asked Oct 07 '09 18:10

Nick Heiner


1 Answers

When calling static generic functions, you don't pass the type parameters:

 HashBiMap<Character, Integer> charOcc = HashBiMap.create();

Also really you shouldn't code to the implementation class, so you're better off doing

 Map<Character, Integer> charOcc = HashBiMap.create();

or

 BiMap<Character, Integer> charOcc = HashBiMap.create();
like image 69
matt b Avatar answered Oct 16 '22 11:10

matt b