Java HashMap clear() Method
Example
Remove all items from a map:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
capitalCities.clear();
System.out.println(capitalCities);
}
}
Definition and Usage
The clear()
method removes all entries from the map.
Syntax
public void clear()
Related Pages
❮ HashMap Methods