Hashtable replace(K, V) in Java

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

Syntax:

V java.util.Hashtable.replace(K key, V value)

This method takes two arguments. This method replaces the entry for the specified key only if it is currently mapped to some value.

Parameters: Two parameters are required for this method.

key: key with which the specified value is associated.

value: value to be associated with the specified key.

Returns: the previous value associated with the specified key, or null if there was no mapping for the key.

Exceptions: NA

Approach

Java

import java.util.Hashtable;

public class Hashtablereplace {
    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 value = 10;
        hashtable.replace(key, value);

        System.out.println(hashtable);

    }
}

Output:

{World=2, Hello=10}


No comments:

Post a Comment