This Java Example shows how to remove all the Entries from the specified HashMap object in java.
Remove All Entries of Java HashMap Example
This Java Example shows how to remove all the Entries from the specified
HashMap object in java. To remove all of the Entries from HashMap object
we use void clear() method. This method removes all of the mappings from this map.
Example: RemoveAllEntriesofHashMap.java
package devmanuals.com; import java.util.*; public class RemoveAllEntriesofHashMap { public static void main(String args[]) { HashMap hm = new HashMap(); hm.put(1, "Gyan"); hm.put(2, "Ankit"); hm.put(3, "Arun"); hm.put(4, "Anand"); hm.put(5, "Ram"); System.out.println("The Entries of map are : " + hm); System.out.println("The size of Map before removal : " + hm.size()); hm.clear(); System.out.println("The size of map after removal :" + hm.size()); } }
Output:
The Entries of map are : {1=Gyan, 2=Ankit, 3=Arun, 4=Anand, 5=Ram} The size of Map before removal : 5 The size of map after removal :0 |
[ 0 ] Comments