Java EnumMap - clear() Method
The java.util.EnumMap.clear() method is used to remove all mappings from this map.
Syntax
public void clear()
Parameters
No parameter is required.
Return Value
void type.
Exception
NA
Example:
In the example below, the java.util.EnumMap.clear() method is used to clear all mappings of the given map.
import java.util.*; public class MyClass { //creating an enum public enum weekday{ SUN, MON, TUE, WED, THU, FRI, SAT } public static void main(String[] args) { //creating an EnumMap EnumMap<weekday,Integer> MyMap = new EnumMap<weekday,Integer>(weekday.class); //associate values in the map MyMap.put(weekday.SUN, 0); MyMap.put(weekday.MON, 1); MyMap.put(weekday.TUE, 2); MyMap.put(weekday.WED, 3); MyMap.put(weekday.THU, 4); MyMap.put(weekday.FRI, 5); MyMap.put(weekday.SAT, 6); //printing EnumMap System.out.println("Before applying clear() method."); System.out.println("MyMap contains: " + MyMap); //using clear method to clear content of the EnumMap MyMap.clear(); //printing EnumMap System.out.println("\nAfter applying clear() method."); System.out.println("MyMap contains: " + MyMap); } }
The output of the above code will be:
Before applying clear() method. MyMap contains: {SUN=0, MON=1, TUE=2, WED=3, THU=4, FRI=5, SAT=6} After applying clear() method. MyMap contains: {}
❮ Java.util - EnumMap