AbstractMap.clear()
is a method of the Java AbstractMap
class. We use it to remove all of the mappings from an AbstractMap
. The AbstractMap
will become empty after we call this method.
AbstractMap.clear()
The AbstractMap.clear()
takes no parameter and
The method throws an UnsupportedOperationException
error if the AbstractMap
does not support the clear()
operation.
We can see what’s illustrated above in the code below.
import java.util.*;class AbstractMapDemo {public static void main(String[] args){// Creating an AbstractMapAbstractMap<Integer, String>abstractMap = new HashMap<Integer, String>();// Mapping string values to int keysabstractMap.put(21, "Shot");abstractMap.put(42, "Java");abstractMap.put(34, "Yaaaayy");// Printing the AbstractMapSystem.out.println("The Mappings are: "+ abstractMap);// Clearing the AbstractMap using clear() methodabstractMap.clear();// Printing the final AbstractMapSystem.out.println("The Final Mappings are: "+ abstractMap);}}