Java HashMap clear() Method

The HashMap.clear() method in Java is used to remove all the key-value pair mappings from a HashMap.

Table of Contents

  1. Introduction
  2. clear Method Syntax
  3. Examples
    • Clearing All Entries from a HashMap
    • Real-World Use Case: Resetting a Country Code Map
  4. Conclusion

Introduction

The HashMap.clear() method is a member of the HashMap class in Java. It allows you to remove all key-value mappings from the HashMap, effectively resetting it to an empty state. This can be useful when you need to reuse the same HashMap instance without retaining any of the previous data.

clear() Method Syntax

The syntax for the clear method is as follows:

public void clear()
  • The method does not take any parameters.
  • The method does not return any value.

Examples

Clearing All Entries from a HashMap

The clear method can be used to remove all entries from a HashMap.

Example

import java.util.HashMap;
import java.util.Map;

public class ClearExample {
    public static void main(String[] args) {
        // Creating a Map with String keys and String values
        Map<String, String> countries = new HashMap<>();

        // Adding entries to the HashMap
        countries.put("India", "IN");
        countries.put("United States", "US");
        countries.put("Canada", "CA");

        // Printing the HashMap before clearing
        System.out.println("HashMap before clear: " + countries);

        // Clearing all entries from the HashMap
        countries.clear();

        // Printing the HashMap after clearing
        System.out.println("HashMap after clear: " + countries);
    }
}

Output:

HashMap before clear: {India=IN, United States=US, Canada=CA}
HashMap after clear: {}

Real-World Use Case: Resetting a Country Code Map

In a real-world scenario, you might use the clear method to reset a map that stores country codes, for example, when you need to reload the data.

Example

import java.util.HashMap;
import java.util.Map;

public class ResetCountryCodeMap {
    public static void main(String[] args) {
        // Creating a Map with String keys (country names) and String values (country codes)
        Map<String, String> countryCodes = new HashMap<>();

        // Adding country codes
        countryCodes.put("India", "IN");
        countryCodes.put("United States", "US");
        countryCodes.put("Canada", "CA");

        // Printing the map before resetting
        System.out.println("Country Codes before reset: " + countryCodes);

        // Resetting the map
        countryCodes.clear();

        // Printing the map after resetting
        System.out.println("Country Codes after reset: " + countryCodes);
    }
}

Output:

Country Codes before reset: {India=IN, United States=US, Canada=CA}
Country Codes after reset: {}

Conclusion

The HashMap.clear() method in Java provides a way to remove all key-value mappings from a HashMap, effectively resetting it to an empty state. By understanding how to use this method, you can efficiently manage the lifecycle of your HashMap instances in your Java applications. This method is useful when you need to clear the contents of a HashMap without creating a new instance, such as resetting maps, clearing caches, or reinitializing data structures.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top