A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/treemap-put-method-in-java/ below:

TreeMap put() Method in Java

TreeMap put() Method in Java

Last Updated : 10 Jul, 2018

The java.util.TreeMap.put() method of TreeMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole.

Syntax:
Tree_Map.put(key, value)
Parameters:

The method takes two parameters, both are of the Object type of the TreeMap.

Return Value:

If an existing key is passed then the previous value gets returned. If a new pair is passed, then NULL is returned. Below programs are used to illustrate the working of java.util.TreeMap.put() Method:

Program 1:

When passing an existing key.

Java
// Java code to illustrate the put() 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);

        // Inserting existing key along with new value
        String returned_value = (String)tree_map.put(20, "All");

        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);

        // Displayin 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, 20=All, 25=Welcomes, 30=You}
Program 2:

When passing a new key.

Java
// Java code to illustrate the put() 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);

        // Inserting existing key along with new value
        String returned_value = (String)tree_map.put(50, "All");

        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);

        // Displayin 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, 50=All}
Note:

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



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4