Clearing a HashMap in Java

In Java, a HashMap is a widely used data structure that stores key-value pairs. Sometimes, we may need to clear the contents of a HashMap to completely remove all the elements from it. In this article, we will discuss different ways to clear a HashMap in Java.

Using the clear() method

The simplest and most straightforward way to clear a HashMap is by using its clear() method. The clear() method removes all the key-value pairs and leaves the HashMap empty.

HashMap<String, Integer> numbers = new HashMap<>();
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);

System.out.println("HashMap before clearing: " + numbers);

numbers.clear();

System.out.println("HashMap after clearing: " + numbers);

Output:

HashMap before clearing: {One=1, Two=2, Three=3}
HashMap after clearing: {}

Creating a new instance

Another way to clear a HashMap is by creating a new instance of it. This approach involves reassigning the HashMap variable to a new object, which effectively discards the previous contents.

HashMap<String, Integer> numbers = new HashMap<>();
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);

System.out.println("HashMap before clearing: " + numbers);

numbers = new HashMap<>();

System.out.println("HashMap after clearing: " + numbers);

Output:

HashMap before clearing: {One=1, Two=2, Three=3}
HashMap after clearing: {}

Removing key-value pairs individually

If you don’t want to clear the entire HashMap but rather remove specific key-value pairs, you can do so using the remove() method. This method takes the key as an argument and removes the corresponding key-value pair from the HashMap.

HashMap<String, Integer> numbers = new HashMap<>();
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);

System.out.println("HashMap before removing key: " + numbers);

numbers.remove("Two");

System.out.println("HashMap after removing key: " + numbers);

Output:

HashMap before removing key: {One=1, Two=2, Three=3}
HashMap after removing key: {One=1, Three=3}

Conclusion

In Java, there are multiple ways to clear a HashMap. The clear() method removes all the key-value pairs, creating an empty HashMap. Alternatively, you can create a new instance of the HashMap or remove individual key-value pairs using the remove() method.

By clearing a HashMap, you can efficiently discard its contents and reuse it for new data without the need for creating a new object.

References: