java.util.HashMap replace(K key, V value) and replace(K key ,V oldValue, V newValue)

Description

On this document we will be showing a java example on how to use the replace() method of HashMap Class. Basically this method is being used to insert a new a new key-value mapping to the hashmap object. The replace method is overloaded and we have two available method and here are the following:

  • replace(K key, V value)
  • replace(K key ,V oldValue, V newValue)

The replace(K key, V value) method accepts a key and a value mapping which will replace the existing value of the key-value mapping by the value method argyment if the key is found on the HashMap otherwise the method will not do anything. Meanwhile the replace(K key, V oldValue, V newValue) will only replace the existing key-value mapping if the key and oldValue is found on the HashMap.

Important Notes of the replace method

  • The replace method has been specified by replace in interface Map<K,V>

Method Syntax

Method 1:

replace(K key, V value)

Method 2:

replace(K key ,V oldValue, V newValue)

Method Argument

The following are the method arguments required for replace(K key, V value)

Data Type Parameter Description
K key key with which the specified value is associated.
V value value to be associated with the specified key

And here are the method arguments and description for the method replace(K key ,V oldValue, V newValue)

Data Type Parameter Description
K key key with which the specified value is associated.
V oldValue value expected to be associated with the specified key
V NewValue value to be associated with the specified key

Method Returns

The replace(K key ,V oldValue, V newValue) method returns true if the value was replaced. Meanwhile the replace(K key, V value) returns the previous value associated with the specified key, or null if there was no mapping for the key. (A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.)

Compatibility

Requires Java 1.2 and up

Java HashMap replace() Example

Below are two java code  that demonstrates the use of replace() method of HashMap class. There are two possible return of the replace method thus the handling of the returned value is totally different.

This example is a lot simpler than it looks. First we have a method init() which generally populates a HashMap object and returned. The init() method has been called by the main method. We have used the replace(K key, V value) to replace the existing id 73654 with a new student name. The returned value were stored into a String result. This is used to evaluate if the operation is successful.

Below is the sample output when you run the above example.

HashMap reaplce(key,value) method example

The above example is similar to the first example however on this case the returned value of the operation is boolean.

Below is the sample output when you run the above example.

HashMap reaplce(key,oldvalue,newValue) method example

Suggested Reading List

References