Open In App

How to Update the Value for an Existing Key in a TreeMap Using put()?

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

TreeMap is a part of Java Collection that can in-built class of java.util package. In Java, TreeMap is a pre-defined class that implements the NavigableMap interface and extends the AbstractMap class.

  • It represents the Key-Value pairs.
  • It is part of the Java Collection Framework.
  • It is a sorted map based on a red-black tree data structure.

In this article, we will be learning how to update the value for an existing key in a TreeMap using the basic put() method in Java.

Syntax of put() method:

TreeMap.put(key, value)

Where,

  • key: It refers to the key element that needs to be inserted into the TreeMap.
  • value: It refers to the value of the mapping key.

Program to Update the Value for an Existing Key in a TreeMap

This method is very simple, and it allows to insertion of a new value for an existing key by using the put() method.

Below is the Program to Update the Value for an Existing Key in a TreeMap Using put() in Java:

Java




// Java program to update the value for
// An existing key in a TreeMap using put() method 
import java.io.*;
import java.util.*;
  
// Driver Class
class GFG {
      // Main Function
    public static void main (String[] args) 
    {       
      // Creating the TreeMap 
      TreeMap<Integer, String> treeMap = new TreeMap<>();
        
      // Inserting the key-pair values into the treeMap
      treeMap.put(1,"GeeksForGeeks");
      treeMap.put(2,"DSA Tutorial");
      treeMap.put(3,"Java Tutorial");
      treeMap.put(4,"Python Tutorial");
        
      // Print the oringinal TreeMap
      System.out.println("Oringinal TreeMap:"+treeMap);
        
      // Updating the values for an existing key
      treeMap.put(2,"Full Stack Web Development");
        
      // Print the updated TreeMap
      System.out.println("Updated TreeMap:"+treeMap);
        
    }
}


Output

Oringinal TreeMap:{1=GeeksForGeeks, 2=DSA Tutorial, 3=Java Tutorial, 4=Python Tutorial}
Updated TreeMap:{1=GeeksForGeeks, 2=Full Stack Web Development, 3=Java Tutorial, 4=Python Tutorial}


Explanation of the above Program:

  • The value for key 2 in the treeMap is used in this program through the usage of the put() method.
  • Then it replaced with Full Stack web Development and the original value DSA Tutorial.
  • At last, it prints the updated TreeMap.


Similar Reads

How to Update the Value of an Existing Key in a LinkedHashMap in Java?
In Java programming, a LinkedHahMap is like HashMap with additional features in the Java Collections framework. It keeps track of the order in which elements were added. A regular HashMap doesn't have a fixed order for elements. LinkedHashMap uses an approach with a doubly-linked list to remember the order of inserted keys. Key terminologies: Key-v
3 min read
Java Program to Get TreeMap Key, Value, or Entry Greater or Less than the Specified Value
TreeMap class is a red-black tree implementation. It helps us in storing key-value pairs in sorted order. Here 3 approaches are discussed as follows where 4 key-value pairs in the TreeMap in all approaches are as follows along with syntax. tree.put100, "=&gt; Welcoming"); tree.put(120, "=&gt; you to "); tree.put(140, "=&gt; computer science portal"
5 min read
TreeMap put() Method in Java
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
3 min read
Java.util.TreeMap.put() and putAll() in Java
The insertion in TreeMap are handled using the variants of the put functions. There are two variants of put() in Java.util.TreeMap, both are discussed in this article. 1. put() : It associates the specified value with the specified key in the map. If a key is already present, then updating it results in updation of that key. Parameters: key : The k
4 min read
How to Get TreeMap Key or Value using Index in Java?
The TreeMap in Java is used to implement the Map interface and NavigableMap along with the AbstractMap Class. The TreeMap is sorted according to the natural ordering of its keys. The TreeMap class is a Red-Black tree implementation of the Map interface and thus does not expose any methods using which we can access the TreeMap keys or values using t
5 min read
How to Replace a Value for the Given Key in the TreeMap?
The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. It is the implementation class of Map interface.It sorts the element on the basis of keys
4 min read
How to Copy Key-Value Pairs from One TreeMap to Another in Java?
In Java, a TreeMap is a Map implementation that stores key-value pairs in a red-black tree structure. It allows insertions and deletions of key-value pairs due to its tree implementation. These operations take O(log n) time on average. In this article, we will be learning how to copy key-value pairs from one TreeMap to another in Java. Syntax:newTr
2 min read
How to Create a TreeMap in Java and Add Key-Value Pairs in it?
In Java, a TreeMap maintains elements in a sorted order as it is an implementation of the SortedMap Interface. It stores key-value pairs in a sorted order. In this article, we will explore the creation of TreeMap in Java and learn the step-by-step process of adding key-value pairs. Program to Create and Add Key-Value Pairs to a TreeMap in JavaBelow
2 min read
How to Implement a Custom Order or Sorting for Key-Value Pairs in a TreeMap in Java?
The Java Collections Framework includes the TreeMap class, in which Java offers a sorted collection of key-value pairs. By default, TreeMap uses a custom Comparator or arranges components according to their natural ordering. In this article, we will learn how to apply a custom order for key-value pairs in a TreeMap. Sorting for Key-Value Pairs in a
2 min read
How to Sort a TreeMap By Value in Java?
In Java Language, a TreeMap always stores key-value pairs which are in sorted order on the basis of the key. TreeMap implements the NavigableMap interface and extends AbstractMap class. TreeMap contains unique keys. Sorting TreeMap by value in Java The elements in TreeMap are sorted on the basis of keys.So, we need to develop our own logic to sort
3 min read