Java: Instantiate Google Collection's HashBiMap

≡放荡痞女 提交于 2019-12-04 03:29:57

问题


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


回答1:


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();


来源:https://stackoverflow.com/questions/1533513/java-instantiate-google-collections-hashbimap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!