put(K key, V value)  method of Map Interface in Java.

put(K key, V value) method of Map Interface in Java.


Posted in : Core Java Posted on : February 18, 2011 at 8:26 PM Comments : [ 0 ]

In this section we will discuss how can put(K key, V value) method be implemented in Map interface in java. V put(K key, V value) This method associates a specific value with the specified key in the map.

put(K key, V value)  method of Map Interface in Java.

In this section we will discuss how can put(K key, V value) method be implemented in Map interface in java.

Syntax

V put(K key, V value) 

This method associates a specific value with the specified key in the map.

In this method the same value can associates with two different keys but, the same key can't be associated with two different values (i.e. key's must be unique but value can be duplicated) in such case, the old value is replaced by the new value. This method gives back the 'value' that was antecedently associated with key, otherwise, returns 'null' if the 'key' had been not mapped. The returning of 'null' signals to the map was antecedently linked 'null' with key.

Parameter description

key : It takes the input as the key through which specified value is to be associated.

value : It takes the input as value to be associated with the specified key. 

Example of put(K key, V value) method

In this example we will show you how does put(K key, V value) method work in Map interface. This example will help you to understand how you can associate a specified value with specified key. In this example we will show you how an old value is replaced with the new value. Further in this example you can also show how this method returns the previous value.  

Example :

package devmanuals.com;
import java.util.Map;
import java.util.HashMap;
public class MapPut {
  public static void main(String args[]) {
    Map<Integer, String> mp = new HashMap<Integer, String>();
    mp.put(1"A");
    mp.put(2"B");
    mp.put(3"C");
    mp.put(4null);
    System.out.println("Mappings into Map are : " + mp);
    System.out.println("Size of Map = " + mp.size());
    // Add the new element into the map;
    mp.put(5"E");
    System.out.println("After putting the new mapping into map, Map = "+ mp);
    System.out.println("Size of Map = " + mp.size());
    String s1 = mp.put(5"F");
    String s = mp.put(4"D");
    // In the next implementation of put()method the old value will be
    // replaced
    // by new value
    System.out.println("Replaces the old value ");
    System.out.println(mp);
    System.out.println("Size of Map = " + mp.size());
    System.out.println("The previous value of 4 was " "'" + s + "'");
    System.out.println("The previous value of 5 was " "'" + s1 + "'");
    // insert a new mapping into map
    String s2 = mp.put(6"G");
    System.out.println(mp);
    System.out.println("The previous value of 6 was " "'" + s2 + "'");
  }
}

Output :

Mappings into Map are : {1=A, 2=B, 3=C, 4=null}

Size of Map = 4

After putting the new element into map, Map = {1=A, 2=B, 3=C, 4=null, 5=E}

Size of Map = 5

Replaces the old value

{1=A, 2=B, 3=C, 4=D, 5=F}

Size of Map = 5

The previous value of 4 was 'null'

The previous value of 5 was 'E'

{1=A, 2=B, 3=C, 4=D, 5=F, 6=G}

The previous value of 6 was 'null'

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics