Sunday, 13 April 2014

putAll : Copies Data from a map to other map

void putAll(Map<? extends K, ? extends V> m)
Copies all of the mappings from the specified map to this map

import java.util.*;

class MapPutAll{
  public static void main(String args[]){
         Map<Integer, String> mapEx1 = new TreeMap<> ();
  Map<Integer, String> mapEx2 = new HashMap<> ();
  
  /* Add Data to mapEx1 */
  mapEx1.put(1, "HI");
  mapEx1.put(2, "How");
  mapEx1.put(3, "Are");
  
  /* Add Data to mapEx1 */
  mapEx2.put(2, "ab");
  mapEx2.put(3, "cd");
  mapEx2.put(4, "ef");
  mapEx2.put(5, "gh");
  
  
  System.out.println("Elements in mapEx1 are");
  System.out.println(mapEx1);
  
  System.out.println("\nElements in mapEx2 are");
  System.out.println(mapEx2);
  
  /*Copying mapEx2 to mapEx1 */
  System.out.println("\nCopying the data from mapEx2 to mapEx1");
  mapEx1.putAll(mapEx2);
  
  System.out.println("\nElements in mapEx1 are");
  System.out.println(mapEx1);
  }
}

Output
Elements in mapEx1 are
{1=HI, 2=How, 3=Are}

Elements in mapEx2 are
{2=ab, 3=cd, 4=ef, 5=gh}

Copying the data from mapEx2 to mapEx1

Elements in mapEx1 are
{1=HI, 2=ab, 3=cd, 4=ef, 5=gh}

1. throws UnsupportedOperationException if the putAll operation is not supported by this map
import java.util.*;

class MapPutAllUnsupported{
  public static void main(String args[]){
         Map<Integer, String> mapEx = new TreeMap<> ();
  Map<Integer, String> unmodifiable;
  
  /* Add Data to mapEx */
  mapEx.put(1, "HI");
  mapEx.put(2, "How");
  mapEx.put(3, "Are");

  unmodifiable = Collections.unmodifiableMap(mapEx);
  
  System.out.println("Elements in unmodifiable are");
  System.out.println(unmodifiable);
  
  /*Copying mapEx to unmodifiable */
  System.out.println("\nCopying the data from mapEx to unmodifiable");
  unmodifiable.putAll(mapEx);
  }
}

Output
Elements in unmodifiable are
{1=HI, 2=How, 3=Are}

Copying the data from mapEx to unmodifiable
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableMap.putAll(Collections.java:1348)
        at MapPutAllUnsupported.main(MapPutAllUnsupported.java:20)

2. throws ClassCastException if the class of a key or value in the specified map prevents it from being stored in this map

3. throws NullPointerException if the specified map is null, or if
this map does not permit null keys or values, and the specified map contains null keys or values
import java.util.*;

class MapPutAllNullPointer{
  public static void main(String args[]){
         Map<Student, Integer> mapEx1 = new HashMap<> ();
  Map<Student, Integer> mapEx2 = new TreeMap<> ();
  Student s1 = new Student("abcd", "efgh");
  
  mapEx1.put(null, 1);
  
  mapEx2.putAll(mapEx1);
  }
}

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1188)
        at java.util.TreeMap.put(TreeMap.java:531)
        at java.util.AbstractMap.putAll(AbstractMap.java:273)
        at java.util.TreeMap.putAll(TreeMap.java:322)
        at MapPutAllNullPointer.main(MapPutAllNullPointer.java:11)
4. throws IllegalArgumentException if some property of a key or value in the specified map prevents it from being stored in this map
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment