Return value put () in HashMap

I had problems understanding the explanation of the return value put()in HashMap:

  private Map<Bookmark, Integer> mDevice = new HashMap<String, Integer>();

    String abc = "two"
    Integer ret = mDevice.put(abc, ONLINE);

Did I say the following correctly:

  1. if the key abc already exists with the value OFFLINE, ret is equal OFFLINE.
  2. if the key abc already exists with the value ONLINE, ret is equal ONLINE.
  3. if the key abc does not exist, then ret is equal null.
+12
source share
4 answers

The put method has a return type with a value:

@Override
    public V put(K key, V value) {
        return putImpl(key, value);
    }

. , .
, , null, . , .

+25
package com.payu.Payu;

import java.util.*;

public class HashMap_Example {
    public static void main(String[] args) {

        // Creating an empty HashMap
        HashMap<Integer, String> hashmap = new HashMap<Integer, String>();

        // Mapping string values to int keys
        hashmap.put(10, "HashMap");
        hashmap.put(15, "4");
        hashmap.put(25, "You");

        // Displaying the HashMap
        System.out.println("Initial Mappings are: " + hashmap);

        // Inserting existing key along with new value
        // return type of put is type of values i.e. String and containing the old value 
        String returned_value = hashmap.put(10, "abc");

        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);

        // Inserting new key along with new value
        // return type of put is type of values i.e. String ; since it is new key ,return value will be null

        returned_value = hashmap.put(20, "abc");

        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);

        // Displayin the new map
        System.out.println("New map is: " + hashmap);
    }
}

:-

: {25=You, 10=HashMap, 15=4}
: HashMap
: null
: {20=abc, 25=You, 10=abc, 15=4}

0

,

0

HashMap put:

, , .

0

All Articles