putAll(Map): This method is available in java.util.IdentityHashMap class of Java.
Syntax:
void java.util.IdentityHashMap.putAll(Map<? extends K, ? extends V> m).
This method 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.
Parameters: One parameter is required for this method.
m: mappings to be stored in this map.
Returns: NA
Throws:
NullPointerException - if the specified map is null
Approach 1: When no exception
Java
import java.util.IdentityHashMap;public class IdentityHashMapputAll {public static void main(String args[]) {IdentityHashMap<String, Integer> identityHashMap =new IdentityHashMap<String, Integer>();identityHashMap.put("Java", 1);identityHashMap.put("Hello", 2);IdentityHashMap<String, Integer> identityHashMap2 =new IdentityHashMap<String, Integer>();identityHashMap2.putAll(identityHashMap);System.out.println(identityHashMap2);}}
Output:
{Java=1, Hello=2}
Approach 2: NullPointerException
Java
import java.util.IdentityHashMap;public class IdentityHashMapputAll {public static void main(String args[]) {IdentityHashMap<String, Integer> identityHashMap = null;IdentityHashMap<String, Integer> identityHashMap2 =new IdentityHashMap<String, Integer>();identityHashMap2.putAll(identityHashMap);System.out.println(identityHashMap2);}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Map.size()" because "m" is null at java.base/java.util.IdentityHashMap.putAll(IdentityHashMap.java:502)
No comments:
Post a Comment