Google Guava 地图与 Java Util 地图
Google Guava Maps vs Java Util Map
在查看开源代码时,我多次遇到类似
的语句
Map<String, List<String>> map = Maps.newHashMap();
其中 Maps
属于软件包 com.google.common.collect.Maps
(Google Guava)
为什么我们不能简单地使用标准:
Map<String, List<String>> map = new HashMap<>();
代替?
这里的要点:在 Java 引入菱形运算符之前,您必须 重复 泛型类型参数,然后才能
Map<Whatever> myMap = new HashMap<Whatever>()
番石榴调用让您避免了重复Whatever
。换句话说:这不过是一种无用(因此被弃用)的便捷方法,因为 Java 具有菱形运算符。它 JavaDoc clearly explains 那。
newHashMap
public static HashMap newHashMap()
…
Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the HashMap constructor directly, taking advantage of the new "diamond" syntax.
您的问题已由 JavaDoc 回答:
Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the HashSet
constructor directly, taking advantage of the new "diamond" syntax.
在查看开源代码时,我多次遇到类似
的语句Map<String, List<String>> map = Maps.newHashMap();
其中 Maps
属于软件包 com.google.common.collect.Maps
(Google Guava)
为什么我们不能简单地使用标准:
Map<String, List<String>> map = new HashMap<>();
代替?
这里的要点:在 Java 引入菱形运算符之前,您必须 重复 泛型类型参数,然后才能
Map<Whatever> myMap = new HashMap<Whatever>()
番石榴调用让您避免了重复Whatever
。换句话说:这不过是一种无用(因此被弃用)的便捷方法,因为 Java 具有菱形运算符。它 JavaDoc clearly explains 那。
newHashMap
public static HashMap newHashMap()
…
Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the HashMap constructor directly, taking advantage of the new "diamond" syntax.
您的问题已由 JavaDoc 回答:
Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the
HashSet
constructor directly, taking advantage of the new "diamond" syntax.