WeakHashMap void putAll(Map m) method Example Program


Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.

Program

package com.candidjava;

import java.util.Map;
import java.util.WeakHashMap;

/* 
 * @author : Mohan 
 * @description : The putAll(Map<? extends K,? extends V> m) method is used to copy all of the mappings from the specified map to this map.
 */
public class WeakHashMapPutAll {
	public static void main(String[] args) {
		Map<String, String> weakHashMapOne = new WeakHashMap<String, String>();
		Map<String, String> weakHashMapTwo = new WeakHashMap<String, String>();

		System.out.println("Populating two Maps");

		weakHashMapOne.put("1", "anandh");
		weakHashMapOne.put("2", "hari");
		weakHashMapOne.put("3", "vinoth");
		weakHashMapTwo.put("4", "kamal");
		weakHashMapTwo.put("5", "karthic");
		weakHashMapTwo.put("6", "mohan");

		System.out.println("Before - Map 1: " + weakHashMapOne);
		System.out.println("Before - Map 2: " + weakHashMapTwo);

		weakHashMapOne.putAll(weakHashMapTwo);

		System.out.println("After - Map 1: " + weakHashMapOne);
		System.out.println("After - Map 2: " + weakHashMapTwo);
	}
}

Output

Populating two Maps
Before - Map 1: {1=anandh, 2=hari, 3=vinoth}
Before - Map 2: {4=kamal, 5=karthic, 6=mohan}
After - Map 1: {4=kamal, 5=karthic, 6=mohan, 1=anandh, 2=hari, 3=vinoth}
After - Map 2: {4=kamal, 5=karthic, 6=mohan}

Explanation

public void putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
Specified by:
putAll in interface Map<K,V>
Overrides:
putAll in class AbstractMap<K,V>
Parameters:
m - mappings to be stored in this map.
Throws:
NullPointerException - if the specified map is null.


Related Post

Comments


©candidjava.com