TreeMap void putAll(Map map) Method Example Program


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

Program

package com.candidjava;

import java.util.TreeMap;

/**
 * @author: Hari Haran
 * @discription: The putAll(Map<? extends K,? extends V> map) method is used to
 *               copy all of the mappings from the specified map to this map.
 *               These mappings replace any mappings that this map had for any
 *               of the keys currently in the specified map.
 */

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

		TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
		TreeMap<Integer, String> tmputall = new TreeMap<Integer, String>();

		tm.put(1, "Anand");
		tm.put(2, "Hari Haran");
		tm.put(8, "Kamal");
		tm.put(4, "Karthikeyan");
		tm.put(5, "Mohan");
		tm.put(6, "Vinoth");
		tm.put(7, "Hari Haran");

		tmputall.put(1, "111");
		tmputall.put(2, "222");
		tmputall.put(7, "333");

		System.out.println("Value before modification: " + tm);

		tm.putAll(tmputall);

		System.out.println("Value after modification: " + tm);
	}
}

Output

Value before modification: {1=Anand, 2=Hari Haran, 4=Karthikeyan, 5=Mohan, 6=Vinoth, 7=Hari Haran, 8=Kamal}
Value after modification: {1=111, 2=222, 4=Karthikeyan, 5=Mohan, 6=Vinoth, 7=333, 8=Kamal}

Explanation

public void putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map. These mappings 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:
map - mappings to be stored in this map
Throws:
ClassCastException - if the class of a key or value in the specified map prevents it from being stored in this map
NullPointerException - if the specified map is null or the specified map contains a null key and this map does not permit null keys


Related Post

Comments


©candidjava.com