[英]Why does System.out.println(map.put(1,“test”)) print null value?
class Test3 {
public static void main(String args[]){
Map<Integer, String> aa = new HashMap();
System.out.println(aa.put(1, "test"));
}
}
whenever I execute this statement it prints null value.每当我执行此语句时,它都会打印 null 值。 I want to know why it printing the null value.我想知道为什么它打印 null 值。 What is happening?怎么了?
From the Javadoc of Map.put
:来自Map.put
的 Javadoc :
V put(K key, V value)
... ...
Returns : the previous value associated with key, or null if there was no mapping for key.返回:与键关联的先前值,如果没有键映射,则返回 null。
The map is empty before the call to put
, so "there was no mapping for key", so null
is returned. map 在调用put
之前为空,因此“没有键映射”,因此返回null
。
implementation of put is something like below put 的实现如下所示
@Override
public V put(K key, V value) {
return putImpl(key, value);
}
If the map previously contained a mapping for the key, the old value is replaced.如果 map 之前包含密钥的映射,则旧值将被替换。 else null will return because there is no mapping.否则 null 将返回,因为没有映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.