Java.util.EnumMap.clear() Method



Description

The java.util.EnumMap.clear() method removes all mappings from this map.

Declaration

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

public void clear()

Parameters

NA

Return Value

This method does not return any value.

Exception

NA

Example

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

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {
   
   // create an enum
   public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; 

   public static void main(String[] args) {
      
      EnumMap<Numbers,String> map = 
         new EnumMap<Numbers,String>(Numbers.class);

      // associate values in map
      map.put(Numbers.ONE, "1");
      map.put(Numbers.TWO, "2");
      map.put(Numbers.THREE,"3");
      map.put(Numbers.FOUR, "4");

      // print the whole map
      System.out.println(map); 

      // clear the mappings
      map.clear();

      // print the map again
      System.out.println(map); 
   }
}

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

{ONE=1, TWO=2, THREE=3, FOUR=4}
{}
java_util_enummap.htm
Advertisements