Sunday 8 June 2014

TreeMap : putAll (Map map)

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

import java.util.*;

class TreeMapPutAll{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  Map<Integer, String> map;
 
  myMap = new TreeMap<> ();
  map = new HashMap<> ();
  
  /* Insert data to myMap */
  map.put(1, "w");
  map.put(2, "wx");
  map.put(3, "wxy");
  map.put(4, "wxy");

  myMap.putAll(map);
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
 }
}

Output
Elements in myMap are
{1=w, 2=wx, 3=wxy, 4=wxy}

1. Throws NullPointerException if the specified map is null or the specified map contains a null key and this map does not permit null keys.

2. Throws ClassCastException if the class 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