Hashtable replace(K, V, V) in Java

replace(K, V, V): This method is available in java.util.Hashtable class of Java.

Syntax:

boolean java.util.Hashtable.replace(K key, V oldValue, V newValue)

This method takes three arguments. This method replaces the entry for the specified key only if currently mapped to the specified value.

Parameters: Three parameters are required for this method.

key: key with which the specified value is associated.

oldValue: value expected to be associated with the specified key.

newValue: value to be associated with the specified key.

Returns: true if the value was replaced.

Exceptions: NA

Approach

Java

import java.util.Hashtable;

public class Hashtablereplace2 {
    public static void main(String[] args) {
        Hashtable<String, Integer> hashtable =
new Hashtable<String, Integer>();

        hashtable.put("Hello", 1);
        hashtable.put("World", 2);

        String key = "Hello";
        Integer oldValue = 1, newValue = 10;
        System.out.println(hashtable.replace(key,
oldValue, newValue));

    }
}

Output:

true


No comments:

Post a Comment