This method is used when we required to delete all the mappings from the underlying map. Whenever this method will call up it will make the map empty.Ã?Â
clear() method of Map Interface in Java.
In this section we will discuss how can clear() method be implemented in Map interface in java.
Syntax
void clear()
This method deletes all of the mappings from the underlying map.
This method is used when we required to delete all the mappings from the underlying map. Whenever this method will call up it will make the map empty.
Parameter description
This method has no parameter.
Example of clear() method
In this example we will show you how does clear() method work in Map interface. This example will help you to understand how can you cleanup the mappings from the underlying map. To clarify the process of this method we will use the size() method that clear us the mappings are removed exactly.
Example :
package devmanuals.com; import java.util.Map; import java.util.HashMap; public class MapClear { public static void main (String args[]){ Map mp =new HashMap(); //Put value into HashMap mp.put(1,"a"); mp.put(2,"b"); mp.put(3,"c"); mp.put(4,"d"); mp.put(5,"e"); System.out.println("Mappings are = "+mp); System.out.println("Size of map = "+mp.size()); //implementation of the method. //this method removes all the mappings from the map mp.clear(); System.out.println("Mappings into map after using clear() method :-"+mp); System.out.println("Size of map = "+mp.size()); } }
Output :
Mappings of map = {1=a, 2=b, 3=c, 4=d, 5=e} Size of map = 5 Mappings into map after using clear() method :-{} Size of map = 0 |
[ 0 ] Comments