What is AbstractMap putAll in Java?
AbstractMap.putAll() is a method of the Java AbstractMap class. We use it to copy all the mappings from a specified AbstractMap to another AbstractMap.
The behaviour of this putAll() operation is undefined if the specified AbstractMap is modified while the operation is in progress.
Syntax
AbstractMap.putAll(anExistingAbstractMap);
Parameters
The AbstractMap.putAll() method takes one parameter, the existingAbstractMap, to be copied.
Throws
The method throws:
-
UnsupportedOperationExceptionwhen theputAll()operation is not supported by the other AbstractMap. -
ClassCastExceptionwhen the class of a key or value in the specified AbstractMap prevents it from being stored in the other AbstractMap. -
NullPointerExceptionif the specified AbstractMap isNULL, or if the other AbstractMap does not permitNULLkeys or values, and the specified AbstractMap containsNULLkeys or values. -
IllegalArgumentExceptionif some property of a key or value in the specified AbstractMap prevents it from being stored in the other AbstractMap.
Code
We can write the illustration above in the code snippet shown below.
import java.util.*;class AbstractMapDemo {public static void main(String[] args){// Creating an AbstractMapAbstractMap<Integer, String>abstractMap = new HashMap<Integer, String>();// Mapping string values to int keysabstractMap.put(21, "Shot");abstractMap.put(42, "Java");abstractMap.put(34, "Yaaaayy");// Printing the AbstractMapSystem.out.println("The Mappings are: "+ abstractMap);// Creating a new AbstractMapAbstractMap<Integer, String>newAbstractMap = new HashMap<Integer, String>();// Copying all the mappingsnewAbstractMap.putAll(abstractMap);// Printing the new AbstractMapSystem.out.println("The new AbstractMap will be: "+ newAbstractMap );}}