Open In App
Related Articles

TreeMap remove() Method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The java.util.TreeMap.remove() is an inbuilt method of TreeMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map. 

Syntax:

Tree_Map.remove(Object key)

Parameters: The method takes one parameter key whose mapping is to be removed from the Map. 

Return Value: The method returns the value that was previously mapped to the specified key if the key exists else the method returns NULL. 

Below programs illustrates the working of java.util.TreeMap.remove() method: 

Program 1: When passing an existing key. 

Java

// Java code to illustrate the remove() method
import java.util.*;
 
public class Tree_Map_Demo {
    public static void main(String[] args)
    {
 
        // Creating an empty TreeMap
        TreeMap<Integer, String> tree_map =
                   new TreeMap<Integer, String>();
 
        // Mapping string values to int keys
        tree_map.put(10, "Geeks");
        tree_map.put(15, "4");
        tree_map.put(20, "Geeks");
        tree_map.put(25, "Welcomes");
        tree_map.put(30, "You");
 
        // Displaying the TreeMap
        System.out.println("Initial Mappings are: "
                                         + tree_map);
 
        // Removing the existing key mapping
        String returned_value = (String)tree_map.remove(20);
 
        // Verifying the returned value
        System.out.println("Returned value is: " +
                                        returned_value);
 
        // Displaying the new map
        System.out.println("New map is: " + tree_map);
    }
}

                    
Output:
Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
Returned value is: Geeks
New map is: {10=Geeks, 15=4, 25=Welcomes, 30=You}

Program 2: When passing a new key. 

Java

// Java code to illustrate the remove() method
import java.util.*;
 
public class Tree_Map_Demo {
    public static void main(String[] args)
    {
 
        // Creating an empty TreeMap
        TreeMap<Integer, String> tree_map =
                    new TreeMap<Integer, String>();
 
        // Mapping string values to int keys
        tree_map.put(10, "Geeks");
        tree_map.put(15, "4");
        tree_map.put(20, "Geeks");
        tree_map.put(25, "Welcomes");
        tree_map.put(30, "You");
 
        // Displaying the TreeMap
        System.out.println("Initial Mappings are: "
                                          + tree_map);
 
        // Removing the new key mapping
        // Note : 50 is not present and so null
        // should be returned (nothing to remove)
        String returned_value = (String)tree_map.remove(50);
 
        // Verifying the returned value
        System.out.println("Returned value is: "
                                   + returned_value);
 
        // Displaying the new map
        System.out.println("New map is: " + tree_map);
    }
}

                    
Output:
Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
Returned value is: null
New map is: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}

Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.



Last Updated : 08 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads