TreeMap remove() Method in Java
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 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); // Displayin the new map System.out.println("New map is: " + tree_map); } } |
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 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); // Displayin the new map System.out.println("New map is: " + tree_map); } } |
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.
Recommended Posts:
- TreeMap put() Method in Java
- TreeMap get() Method in Java
- TreeMap clone() Method in Java
- TreeMap containsKey() Method in Java
- TreeMap clear() Method in Java
- TreeMap containsValue() Method in Java
- TreeMap size() Method in Java
- TreeMap keySet() Method in Java
- TreeMap entrySet() Method in Java
- TreeMap headMap() Method in Java
- TreeMap putAll() Method in Java
- TreeMap subMap() Method in Java
- TreeMap lastKey() Method in Java
- TreeMap values() Method in Java
- TreeMap comparator() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vaibhav2992



