HashTable void putAll(Map t) Method Example Program


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

Program

package com.candidjava;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @author: Hari Haran
 * @discription: The putAll(Map<? extends K,? extends V> t) method is used to
 *               copy all of the mappings from the specified Map to this
 *               Hashtable.
 */

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

		Hashtable<Integer, String> ht1 = new Hashtable<Integer, String>();

		Map<Integer, String> m = new HashMap<Integer, String>();

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

		System.out.println("Initial hash table value: " + ht1);
		System.out.println("Map values: " + m);

		// put map values in table
		ht1.putAll(m);
		System.out.println("Hash table value after put all: " + ht1);
	}
}

Output

Initial hash table value: {}
Map values: {1=Anand, 2=Hari Haran, 3=Kamal, 4=Karthikeyan, 5=Mohan, 6=Vinoth, 7=Hari Haran}
Hash table value after put all: {7=Hari Haran, 6=Vinoth, 5=Mohan, 4=Karthikeyan, 3=Kamal, 2=Hari Haran, 1=Anand}

Explanation

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



Related Post

Comments


©candidjava.com