5

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

Community
  • 1
  • 1
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699

1 Answers1

20

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();
matt b
  • 138,234
  • 66
  • 282
  • 345
  • can you explain your point about "you shouldn't code to the implementation class?" – Nick Heiner Oct 07 '09 at 19:16
  • 10
    More than likely, the rest of your code only cares that you are using a Map/BiMap (which are both interfaces), not that you are specifically using a HashBiMap vs TreeBiMap (not sure if this exists) or some other BiMap implementation. By coding to the interface and not the implementation, if you ever want to swap out the underlying implementation you use (let's say you decide you should be using a TreeMap instead of a HashMap, because you want ordering), then you only have to make changes in a very few places. – matt b Oct 07 '09 at 19:21
  • 5
    Incidentally, your original code would work too if you just move the dot before the type parameters. – Kevin Bourrillion Nov 05 '09 at 20:15