Sunday 13 April 2014

remove : Remove from Map

V remove(Object key)
Removes the mapping for a key from this map if it is present. Returns the value to which this map previously associated the key, null if the map contained no mapping for the key.

import java.util.*;

class MapRemove{
  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 mapEx map are");
  System.out.println(mapEx);
  
  System.out.print("\nRemoving the key 1 and mapped value ");
  System.out.println(mapEx.remove(1));
  
  System.out.println("Elements in mapEx map are");
  System.out.println(mapEx);
  
  System.out.print("\nRemoving the key 4 and mapped value ");
  System.out.println(mapEx.remove(4));
  
  System.out.println("Elements in mapEx map are");
  System.out.println(mapEx);
  }
}

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

Removing the key 1 and mapped value HI
Elements in mapEx map are
{2=How, 3=Are}

Removing the key 4 and mapped value null
Elements in mapEx map are
{2=How, 3=Are}

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

import java.util.*;

class MapRemoveUnsupported{
  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);
  
  unmodifiable.remove(5);
  }
}

Output
Elements in unmodifiable map are
{1=HI, 2=How, 3=Are}
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableMap.remove(Collections.java:1345)
        at MapRemoveUnsupported.main(MapRemoveUnsupported.java:18)

2. throws ClassCastException if the key is of an inappropriate type for
this map
class Student{
  String firstName, lastName;
 
  Student(String firstName, String lastName){
    this.firstName = firstName;
  this.lastName  = lastName;
  }
}

import java.util.*;

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

Output
Exception in thread "main" java.lang.ClassCastException: Student cannot be cast
to java.lang.Comparable
        at java.util.TreeMap.getEntry(TreeMap.java:343)
        at java.util.TreeMap.remove(TreeMap.java:595)
        at MapRemoveClassCast.main(MapRemoveClassCast.java:8)
To Make the program run, Student class must implements the Comparable interface.

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

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

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.getEntry(TreeMap.java:342)
        at java.util.TreeMap.remove(TreeMap.java:595)
        at MapRemoveNullPointer.main(MapRemoveNullPointer.java:7)






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment