The Java.util.EnumMap.clear() method in Java is used to remove all the mappings from the map. The method does not delete the map instead it just clears the map of the mappings.
Syntax:
enum_map.clear()
Parameters: This method does not accept any argument.
Return Values: This method does not return any value.
Below programs illustrate the working of Java.util.EnumMap.clear() method:
Program 1:
// Java program to demonstrate clear() method import java.util.*; // An enum of geeksforgeeks ranking across // Worldwide & in India is created public enum gfg { Global, India } ; class Enum_map { public static void main(String[] args) { EnumMap<gfg, Integer> mp = new EnumMap<gfg, Integer>(gfg.class); // Values are associated in mp mp.put(gfg.Global, 800); mp.put(gfg.India, 72); // Values in mp before removing System.out.println("Values in map before removing " + mp); // Removing the values from mp mp.clear(); // Values in mp after removing System.out.println("Values in map after removing " + mp); } } |
Values in map before removing {Global=800, India=72}
Values in map after removing {}
Program 2:
// Java program to demonstrate clear() method import java.util.*; // An enum of fruits price is created public enum Price_of_Fruits { Orange, Apple, Banana, Pomegranate, Guava } ; class Enum_map { public static void main(String[] args) { EnumMap<Price_of_Fruits, Integer> mp = new EnumMap<Price_of_Fruits, Integer>(Price_of_Fruits.class); // Values are associated in mp mp.put(Price_of_Fruits.Orange, 30); mp.put(Price_of_Fruits.Apple, 50); mp.put(Price_of_Fruits.Banana, 40); mp.put(Price_of_Fruits.Pomegranate, 120); mp.put(Price_of_Fruits.Guava, 20); // Values in mp before removing System.out.println("Values in map before removing " + mp); // Removing the values from mp mp.clear(); // Values in mp after removing System.out.println("Values in map after removing " + mp); } } |
Values in map before removing {Orange=30, Apple=50, Banana=40,
Pomegranate=120, Guava=20}
Values in map after removing {}
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- EnumMap equals() Method in Java with Examples
- Set clear() method in Java with Examples
- LongBuffer clear() method in Java with Examples
- IntBuffer clear() method in Java with Examples
- Calendar clear() Method in Java with Examples
- AbstractList clear() method in Java with Examples
- LinkedHashSet clear() method in Java with Examples
- AbstractQueue clear() method in Java with examples
- SortedMap clear() method in Java with Examples
- AbstractMap clear() Method in Java with Examples
- ConcurrentSkipListMap clear() method in Java with Examples
- List clear() method in Java with Examples
- Collection clear() method in Java with Examples
- Properties clear() method in Java with Examples
- AbstractCollection clear() Method in Java with Examples
- SortedSet clear() method in Java with Examples
- Calendar clear(int cal_field) Method in Java with Examples
- Locale.Builder clear() method in Java with Examples
- EnumMap put() Method in Java
- EnumMap get() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

