Java HashMap初始化?
我已经知道如何使用以下两种方法之一初始化JavaJava HashMap初始化?,java,collections,hashmap,declaration,sonarlint,Java,Collections,Hashmap,Declaration,Sonarlint,我已经知道如何使用以下两种方法之一初始化JavaHashMap // 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 Has
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();
哪一个是最好的?为什么?使用Java 7菱形运算符:
HashMap<String, Integer> hashMap2 = new HashMap<>();
HashMap hashMap2=newhashmap();
Diamond允许编译器隐式推断类型
请参阅:相关问题相关问题
newhashmap()代码>未使用菱形运算符。它应该是newhashmap()代码>非常感谢@new HashMap();java 5或6,新的hashmap();Java 7及以后版本
new HashMap();
HashMap<String, Integer> hashMap2 = new HashMap<>();