replace(K key, V value) method

replace(K key, V value): This replace string Javascript method is used to replace the entry for the specified key only if it is currently mapped to some value in java.

V replace(K key, V value)

Where, Key is the key with which the specified value(V) is to be associated in TreeMap.
Value is the value to be associated with the specified key(K) in TreeMap.

import java.util.TreeMap;

public class ExampleOfTreeMap 
{
	public static void main(String[] args) 
	{
		TreeMap<Integer,String> listOfNames = new TreeMap<Integer, String>();
		
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "RAVI");
		
		listOfNames.replace(2, "Hello");
		
		System.out.println("Value of key 2 = "+listOfNames.get(2));
		
	}
}

Output: Value of key 2 = Hello

Leave a Comment