Java:实例化Google Collection的HashBiMap [英] Java: Instantiate Google Collection's HashBiMap

查看:516
本文介绍了Java:实例化Google Collection的HashBiMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Eclipse,并且我已将 google-collect.1.0-rc2.jar 添加为引用库。但不知怎的,这仍然不起作用:

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出现以下错误:

Eclipse gives the following errors:


此行的多个标记

Multiple markers at this line


  • 无法解析HashBiMap

  • 无法解析Character.Integer

  • 令牌,,。上的语法错误。预期

  • 令牌。上的语法错误,删除此令牌

  • 类Odp的方法create()未定义

  • 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

我做错了什么?

其他谷歌的东西,像乔纳一样,工作得很好。 (但Joiner不是通用的。)

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

推荐答案

调用静态泛型函数时,不传递类型参数:

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

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

这篇关于Java:实例化Google Collection的HashBiMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆