putAll() method of Map Interface in Java.

putAll() method of Map Interface in Java.


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

In this section we will discuss how can putAll() method be implemented in Map interface in Java. void putAll(Map m) This method transcripts all the 'key-value' association from a specific map to the underlying map.

putAll() method of Map Interface in Java.

In this section we will discuss how can putAll() method be implemented in Map interface in Java.

Syntax

void putAll(Map<? extends K,? extends V> m) 

This method transcripts all the 'key-value' association from a specific map to the underlying map.

Calling of this method puts effect equivalent to the call of put() method onto the underlying map once for each mapping from key 'k' to value 'v' into the specified map. The behavior of this operation is uninfluenced if the specified collection is modified while the operation is in progress.

Parameter description

m : It takes an argument as mappings to be stored into the underlying map.

Example of putAll(Map m) method 

In this example we will show you how does putAll(Map m) method work in Map interface. This example will help you to understand how you can copy all the mappings from the specified map to the underlying map. Further we will see whether there is any change occurred in the specified map, if we do some operation such as adding element into this map after copying the specified map.     

Example :

package devmanuals.com;
import java.util.Map;
import java.util.HashMap;
public class MapPutAll {
  public static void main(String args[]) {
    Map mp = new HashMap();
    mp.put(1"A");
    mp.put(2"B");
    mp.put(3"C");
    mp.put(4"D");
    System.out.println("Mappings into Map (mp) are  : " + mp);
    System.out.println("Size of Map (mp) = " + mp.size());
    Map mp1 = new HashMap();
    mp1.putAll(mp);
    mp1.put(5"E");
    System.out.println("Mappings into Map (mp1) after copying all the maps from Map (mp) =");
    System.out.println(mp1);
    System.out.println("Size of Map (map1) = " + mp1.size());
  }
}

Output :

Mappings into Map (mp) are : {1=A, 2=B, 3=C, 4=D}

Size of Map = 4

Mappings into Map (mp1) after copying all the maps from Map mp =

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

Size of map1 = 5

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics