java.util.HashMap.clear() Method


Advertisements


Description

The clear() method is used to remove all of the mappings from this map.

Declaration

Following is the declaration for java.util.HashMap.clear() method.

public void clear()

Parameters

  • NA

Return Value

  • NA

Exception

  • NA

Example

The following example shows the usage of java.util.HashMap.clear()

package com.tutorialspoint;

import java.util.*;

public class HashMapDemo {
   public static void main(String args[]) {
   // create hash map
   HashMap newmap = new HashMap();
      
   // populate hash map
   newmap.put(1, "tutorials");
   newmap.put(2, "point");
   newmap.put(3, "is best"); 
      
   System.out.println("Initial map elements: " + newmap);
      
   // clear hash map
   newmap.clear();
      
   System.out.println("Map elements after clear: " + newmap);
   }    
}

Let us compile and run the above program, this will produce the following result.

Initial map elements: {1=tutorials, 2=point, 3=is best}
Map elements after clear: {}

java_util_hashmap.htm

Advertisements