Open In App

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. 

Ways by which we can replace the value using the given key:



  1. Using put() method of TreeMap class
  2. Using replace() method of TreeMap class
  3. Using ComputeIfPresent() method of TreeMap class

1. put() method:

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.




// Java program  to replace a value for
// the given key in the TreeMap
 
import java.io.*;
import java.util.TreeMap;
class GFG {
    public static void main (String[] args) {
         
      TreeMap<String,Integer> gfg=new TreeMap<>();
       
      //adding values to the treemap
       
      gfg.put("dsa",99);
      gfg.put("interview",98);
      gfg.put("fang",80);
       
      //for the fang key we will change the value from 80 to 99
      //using put method
      gfg.put("fang",99);
       
      for(String key: gfg.keySet()){
      System.out.println(key+" , "+gfg.get(key));
      }
    }
}

Output
dsa , 99
fang , 99
interview , 98

2. Using replace() method :

Syntax:

public V replace(K key, V value)

Parameters: This method accepts two parameters:

Return Value: This method returns the previous value associated with the specified key. If there is no such key mapped, then it returns null, if the implementation supports null value.

Exceptions: This method will throw:




// Java program  to replace a value
// for the given key in the TreeMap
 
import java.io.*;
import java.util.TreeMap;
class GFG {
    public static void main (String[] args) {
         
      TreeMap<String,Integer> gfg=new TreeMap<>();
       
      //adding values to the treemap
       
      gfg.put("dsa",99);
      gfg.put("interview",98);
      gfg.put("fang",80);
       
      //here we are using dsa key to change its value from 99 to 100
      gfg.replace("dsa",100);
       
      for(String key: gfg.keySet()){
      System.out.println(key+" , "+gfg.get(key));
      }
    }
}

Output
dsa , 100
fang , 80
interview , 98

3. computeIfPresent() method:




// Java program  to replace a value
// for the given key in the TreeMap
 
import java.io.*;
import java.util.TreeMap;
class GFG {
    public static void main (String[] args) {
         
      TreeMap<String,Integer> gfg=new TreeMap<>();
       
      //adding values to the treemap
       
      gfg.put("dsa",99);
      gfg.put("interview",98);
      gfg.put("fang",80);
       
      //here we are using interview key to change its value from 98 to 50
      gfg.computeIfPresent("interview",(k,v)->50);
       
      for(String key: gfg.keySet()){
      System.out.println(key+" , "+gfg.get(key));
      }
    }
}

Output
dsa , 99
fang , 80
interview , 50

Article Tags :