I was having trouble understanding the explanation for return value of put()
in a HashMap
:
private Map<Bookmark, Integer> mDevice = new HashMap<String, Integer>();
String abc = "two"
Integer ret = mDevice.put(abc, ONLINE);
Am I correct in saying the following:
OFFLINE
, ret is equal to OFFLINE
.ONLINE
, ret is equal to ONLINE
.null
.put(key,value) is return type of 'value' in hashmap.
Return Value The puts() function returns EOF if an error occurs. A nonnegative return value indicates that no error has occurred.
HashMap put() Method in Java put method returns the previous value associated with the specified key if there was mapping for the specified key else it returns null. Syntax. Object put(K key, V value) Parameters key - key for the entry, value - value to be associated with the key.
The method put has a return type same with the value:
@Override
public V put(K key, V value) {
return putImpl(key, value);
}
The method associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
It returns the previous value associated with key, or null if there was no mapping for key.So, your points are right.
For more details please visit here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With