Java Collection Tutorial - Java HashMap.clear()


HashMap.clear() has the following syntax.

public void clear()


In the following code shows how to use HashMap.clear() method.

/*  ww w .  ja  va 2  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("Initial map elements: " + newmap);
      // clear hash map
      System.out.println("Map elements after clear: " + newmap);

The code above generates the following result.