Wednesday 4 June 2014

HashMap : remove : remove Mapping

public V remove(Object key)
Removes the mapping for the specified key from this map if present. Return the previous value associated with key, or null if there was no mapping for key.

import java.util.*;

class HashMapRemove{
 public static void main(String args[]){
  HashMap<Integer, String> myMap;
  myMap = new HashMap<> ();
  
  /* Add Data to myMap */
  myMap.put(100, "Hi");
  myMap.put(25, "How");
  myMap.put(31, "Are");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.print("Removing the Mapping for the Key 25 ");
  System.out.println(myMap.remove(25));
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
 }
}


Output
Elements in myMap are
{100=Hi, 25=How, 31=Are}
Removing the Mapping for the Key 25 How
Elements in myMap are
{100=Hi, 31=Are}


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment