Open In App

Java TreeMap clear() Method

Last Updated : 16 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The clear() method is a built-in method of the TreeMap class in Java of the java.util package. This method is used to remove all key-value mappings from the TreeMap. And after invoking this method, the map becomes completely empty.

This method is very useful when we want to reset the map or discard all the existing entries without creating a new instance.

Syntax of TreeMap clear() Method

treeMap.clear();

  • Parameters: This method does not accept any parameters.
  • Return Value: This method does not return any value. It only removes all the entries from the map.

Examples of TreeMap clear() Method

Example 1: In this example, we are going to create a TreeMap with Integer keys and String values and then clear it using the clear() method.

Java
// Java program to demonstrate TreeMap clear() method
import java.util.TreeMap;

public class Geeks {
    
    public static void main(String[] args) {
        
        // Create a TreeMap with Integer keys
        TreeMap<Integer, String> tm = new TreeMap<>();

        // Add key-value pairs
        tm.put(10, "Geeks");
        tm.put(15, "4");
        tm.put(20, "Geeks");
        tm.put(25, "Welcomes");
        tm.put(30, "You");

        System.out.println("Initial Mappings are: " + tm);

        // Clear the TreeMap
        tm.clear();

        System.out.println("TreeMap after clear(): " + tm);
    }
}

Output
Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
TreeMap after clear(): {}

Explanation: Here, we have added entries to the map then we use the clear() method to remove all mappings. Then the TreeMap becomes empty.


Example 2: In this example, we are using a TreeMap where keys are of type String and values are of type Integer.

Java
// Java program to demonstrate TreeMap clear() method
import java.util.TreeMap;

public class Geeks {
    
    public static void main(String[] args) {
        
        // Create a TreeMap with String keys
        TreeMap<String, Integer> tm = new TreeMap<>();

        // Add key-value pairs
        tm.put("Geeks", 10);
        tm.put("4", 15);
        tm.put("Geeks", 20); 
        tm.put("Welcomes", 25);
        tm.put("You", 30);

        System.out.println("Initial Mappings are: " + tm);

        // Clear the TreeMap
        tm.clear();

        System.out.println("TreeMap after clear(): " + tm);
    }
}

Output
Initial Mappings are: {4=15, Geeks=20, Welcomes=25, You=30}
TreeMap after clear(): {}

Explanation: Here also the clear() method deletes all mappings from the map.

Important Points:

  • The clear() method is used to remove all mappings from a TreeMap.
  • This method does not return any value.
  • After clearing, the map size becomes zero.
  • This method works with all combinations of key-value types i.e., Integer, String, etc.
  • This method is very useful when reusing the same map instance in different logic blocks.

Next Article

Similar Reads