Sunday 13 April 2014

put : Insert Key, value to map

V put(K key, V value)
Insert the key, value mapping to the map. If the map previously contained a mapping for the key, the old value is replaced by the specified value. Return the previous value associated with key, or null if there was no mapping for key.

import java.util.*;

class MapPut{
  public static void main(String args[]){
    Map<Integer, String> mapEx = new TreeMap<> ();
  
  /* Add Data to mapEx */
  mapEx.put(1, "HI");
  mapEx.put(2, "How");
  mapEx.put(3, "Are");
  
  System.out.println("Elements in Map are " + mapEx);
  
  System.out.println("\nUpdating the Value of the key 1");
  System.out.println("Privious Value " + mapEx.put(1, "abcd"));
  System.out.println("New Value is " + mapEx.get(1));
  
  System.out.println("\nElements in Map are " + mapEx);
  
  System.out.println("\nInsert New Key 4");
  System.out.println("Privious Value " + mapEx.put(4, "wxyz"));
  System.out.println("New Value is " + mapEx.get(4));
  
  System.out.println("\nElements in Map are " + mapEx);
  }
}

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

Updating the Value of the key 1
Privious Value HI
New Value is abcd

Elements in Map are {1=abcd, 2=How, 3=Are}

Insert New Key 4
Privious Value null
New Value is wxyz

Elements in Map are {1=abcd, 2=How, 3=Are, 4=wxyz}

1. throws UnsupportedOperationException if the put operation is not supported by this map

import java.util.*;

class MapPutUnsupported{
  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 map are");
  System.out.println(unmodifiable);
  
  System.out.println("Trying to update data of unmodifiable");
  unmodifiable.put(1, "HI");
  }
}

Output
Elements in unmodifiable map are
{1=HI, 2=How, 3=Are}
Trying to update data of unmodifiable
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableMap.put(Collections.java:1342)
        at MapPutUnsupported.main(MapPutUnsupported.java:19)

2. throws ClassCastException if the class of the specified key or value
prevents it from being stored in this map
class Student{
  String firstName, lastName;
 
  Student(String firstName, String lastName){
    this.firstName = firstName;
  this.lastName  = lastName;
  }
}

import java.util.*;

class MapPutClassCast{
  public static void main(String args[]){
    Map<Student, Integer> mapEx = new TreeMap<> ();
  Student s1 = new Student("abcd", "defg");
  
  mapEx.put(s1, 1);
  }
}

Output
Exception in thread "main" java.lang.ClassCastException: Student cannot be cast
to java.lang.Comparable
        at java.util.TreeMap.compare(TreeMap.java:1188)
        at java.util.TreeMap.put(TreeMap.java:531)
        at MapPutClassCast.main(MapPutClassCast.java:8)

To Make the program run, Student class must implements the Comparable interface.

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

class MapPutNullPointer{
  public static void main(String args[]){
    Map<Integer, String> mapEx = new TreeMap<> ();
  
  mapEx.put(null, "abcd");
  }
}

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 MapPutNullPointer.main(MapPutNullPointer.java:7)

4. throws IllegalArgumentException if some property of the specified key or value prevents it from being stored in this map






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment