Java Collection Tutorial - Java HashMap.remove(Object key)


HashMap.remove(Object key) has the following syntax.

public V remove(Object key)


In the following code shows how to use HashMap.remove(Object key) method.

/*from  www  .  ja  v  a2 s.  c o  m*/
import java.util.HashMap;

public class Main {
   public static void main(String args[]) {
      HashMap<Integer, String> newmap = new HashMap<Integer, String>();      
      // populate hash map
      newmap.put(1, "tutorials");
      newmap.put(2, "from");
      newmap.put(3, "");
      System.out.println("Values before remove: "+ newmap);
      // remove value for key 2

      System.out.println("Values after remove: "+ newmap);

The code above generates the following result.