【发布时间】:2018-12-20 08:42:15
【问题描述】:
我已经知道如何使用以下 2 种方式之一来初始化 Java HashMap
// way 1: apply generic type saftey
HashMap<String, Integer> hashMap1 = new HashMap<String, Integer>();
// way 2: general without apply generic type saftey
HashMap<String, Integer> hashMap2 = new HashMap();
我的问题
最佳做法是什么
根据Eclipse Marker
类型安全:HashMap 类型的表达式需要未经检查的转换 符合HashMap
new HashMap<String, Integer>();
但根据 Sonar Linter
将此构造函数调用中的类型规范替换为 菱形运算符(“”)。
new HashMap();
哪个是最好的?为什么?
【问题讨论】:
-
new HashMap();没有使用菱形运算符。应该是new HashMap<>(); -
非常感谢@Eran
-
new HashMap(); java 5 或 6 , 新的 hashmap(); Java 7 及以上版本
标签: java collections hashmap declaration sonarlint