How to clear Hashmap in java
To clear HashMap in java we use the
void clear() method from Map.
Using the clear method will empty the HashMap. Lets see examples on how to clear Hashmap in java.
Example : Clear HashMap in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.kscodes.sampleproject; import java.util.HashMap; import java.util.Map; public class HashMapExample { public static void main(String[] args) { Map<String, String> hashMap = new HashMap<String, String>(); hashMap.put("USD", "American Dollar"); hashMap.put("AUD", "Australian Dollar"); hashMap.put("EUR", "Euro"); hashMap.put("JPY", "Japan Yen"); System.out.println("Hashmap size is :: " + hashMap.size()); System.out.println("Now use Clear method"); hashMap.clear(); System.out.println("Hashmap size After Clear method :: " + hashMap.size()); } } |
Output
1 2 3 | Hashmap size is :: 4 Now use Clear method Hashmap size After Clear method :: 0 |