首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

为啥不能直接new HashMap<key, value>().put(key, value)?

2013-06-25 
为什么不能直接new HashMapkey, value().put(key, value)???MapLong, Double map new HashMapLong,

为什么不能直接new HashMap<key, value>().put(key, value)???


Map<Long, Double> map = new HashMap<Long, Double>().put(11L, 22D);//compile error

Map<Long, Double> map = new HashMap<Long, Double>();
map.put(11L, 22D);//correct

提示:
Type mismatch: cannot convert from Double to Map<Long,Double>
怎么识别成Double去了
[解决办法]
引用:
Java code

Map<Long, Double> map = new HashMap<Long, Double>().put(11L, 22D);//compile error

Map<Long, Double> map = new HashMap<Long, Double>();
map.put(11L, 22D);//correct


提示:
Type mismatch: c……


put方法返回值是Double,又怎么可能转为Map<Long, Double>?
编译报那么明显的错误,你还问?

热点排行