Java中的泛型有时会很吵.编译后,编译器抛出参数化类型,所以我的问题是,忽略它们是否真的有任何缺点,只要我声明包含参数化类型的引用类型?例如,
Map<String, Map<Integer, String>> myMap = new HashMap<String, Map<Integer,String>>();
Run Code Online (Sandbox Code Playgroud)
太吵了.如果我写的怎么办:
@SuppressWarnings("unchecked")
Map<String, Map<Integer, String>> myMap = new HashMap();
Run Code Online (Sandbox Code Playgroud)
它是如此清晰,除了Eclipse将在它下面放一个令人作呕的波浪线.把@SuppressWarning放到Eclipse会很开心,但很多人(包括我)都不喜欢在代码中使用@SuppressWarning.忽略"new HashMap()"中的类型参数真的那么糟糕吗?
Google Collections拯救了!
使用Maps.newHashMap - >
import com.google.common.collect.Maps;
Map<X, Map<Y, Z>> myAwesomeMap = Maps.newHashMap();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
181 次 |
| 最近记录: |