【发布时间】:2018-04-16 00:54:07
【问题描述】:
我不明白为什么在将Collections.emptyMap() 分配给地图引用时将Collections.emptyMap() 作为参数传递时出现错误,但没有提供错误,下面是我尝试的代码示例,我使用的是JDK1。 7
public class Solution {
public static void main(String[] args) {
Solution sol = new Solution();
Map<String, String> map = Collections.emptyMap(); //There is no compile time error on this.
sol.calculateValue(Collections.emptyMap()); //Getting compile time error on this
}
//what is the difference in passing Collections.emptyMap() as a parameter
public void calculateValue(Map<String, String> pMap) {
}
}
【问题讨论】:
-
我似乎记得 Java 1.7,在某些情况下泛型类型推断存在问题。在
return (x != null ? x : Collections.emptyMap())之类的地方使用它也会出现类似的问题,也无法编译。
标签: java generics collections