3:28 pm - Sunday August 20, 2017

EnumMap String and Number in Same using PutAll() method

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This Program is used EnumMap String and Number in Same by using PutAll() method.

Example Program


package com.example;

package com.example;
import java.util.*;

public class EnumMapPutAll { // create an enum
	public enum Numbers {
		ONE, TWO, THREE, FOUR, FIVE
	};

	public static void main(String[] args) {
		EnumMap<Numbers, String> map1 = new EnumMap<Numbers, String>(
				Numbers.class);
		EnumMap<Numbers, String> map2 = new EnumMap<Numbers, String>(
				Numbers.class);
		// associate values in map1
		map1.put(Numbers.ONE, "1");
		map1.put(Numbers.TWO, "2");
		map1.put(Numbers.THREE, "3");
		map1.put(Numbers.FOUR, "4"); // print the maps
		System.out.println("Map1: " + map1);
		System.out.println("Map2: " + map2);// put all mapping from map1 to map
											// 2
		map2.putAll(map1); // print the maps
		System.out.println("Map1: " + map1);
		System.out.println("Map2: " + map2);
	}
}

Output:

Map1:{1=ONE, 2=TWO, 3=THREE, 4=FOUR}

Map2:{}

Map1:{1=ONE, 2=TWO, 3=THREE, 4=FOUR}

Map2:{1=ONE, 2=TWO, 3=THREE, 4=FOUR}

Description

The java.util.EnumMap.putAll(Map<? extends K,? extends V> m) method copies all of the mappings from the specified map to this map. Older values are replaced.

Declaration

Following is the declaration for java.util.EnumMap.putAll() method

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

Parameters

  • m — the mappings to be stored in this map

Return Value

This method does not return any value.

Exception

  • NullPointerException — the specified map is null, or if one or more keys in the specified map are null

BACK

Filed in: Z-A All Java Codes

Comments are closed.