In this section we will discuss how can replace(k,v) method be implemented in ConcurrentMap interface in java. V replace(key, value) This method replaces the mapping for a key only if there is presently mapped to some value.
ConcurrentMap's replace(k,v) method in Java.
In this section we will discuss how can replace(k,v) method be implemented in ConcurrentMap interface in java.
Syntax
V replace(key, value)
This method substitutes the mapping for a key only if there is presently mapped to some value.
The basic work of this method is to replace the value. Internally this function first do test for the key whether it is presently mapped into the map, if it is true then it puts the new value in place of the old value which had been associated early with the key and returns the earlier associated (old) value otherwise, returns 'null' if there was no association for the key. If this function returns 'null' it does not only mean that the map has no association for the specified key this can also meant that the particular key is associated with 'null' value.
Parameter description
key : It is the key with which the value associates.
value : It is the value that is to be associated with the particular key.
Example of replace(k,v) method
In this example we will show you how does replace(k,v) method work in ConcurrentMap interface. This example will help you to understand how can you replace the previously existing value that is associated with the specified key. Through this example we will also show you what it returns when the value is replaced successfully and what does it return when there is no mapping for the given key.
Example :
package devmanuals.com; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentHashMap; import java.util.Set; class CMReplace1 implements Runnable { ConcurrentMapcmp; String name; public CMReplace1(String name, ConcurrentMap cmp) { this.cmp = cmp; this.name = name; } public void run() { System.out.println(name + " maps the element "); cmp.put(1, "A"); cmp.put(3, "C"); cmp.put(4, "D"); System.out.println(cmp); System.out.println("Size of Map = " + cmp.size()); Set s = cmp.keySet(); System.out.println("Key set of map = " + s); // here implementation of this method will show the previously // associated value with specified key. String s2 = cmp.replace(2, "E"); System.out.println("Previously associated value of '2' was : " + "'" + s2 + "'"); try { Thread.sleep(400); } catch (InterruptedException e) { e.printStackTrace(); } } } class CMReplace2 implements Runnable { String name; ConcurrentMap cmp; public CMReplace2(String name, ConcurrentMap cmp) { this.cmp = cmp; this.name = name; } public void run() { try { // here implementation of method will return the previously // associated value. cmp.put(2, "E"); String s = cmp.replace(2, "D"); System.out.println("Previously associated value of '2' was : " + "'" + s + "'"); // here implementation of method will return null it shows //there is no mapping exist into the map. String s1 = cmp.replace(2, "B"); System.out.println("Previously associated value of '2' was : " + "'" + s1 + "'"); Thread.sleep(1500); System.out.println("Set of key = " + cmp.keySet()); System.out.println("Size of Map = " + cmp.size()); } catch (InterruptedException e) { e.printStackTrace(); } } } public class CMReplace { public static void main(String args[]) { ConcurrentMap cmp = new ConcurrentHashMap (); Runnable a = new CMReplace1("A", cmp); new Thread(a).start(); try { Thread.sleep(400); } catch (InterruptedException e) { e.printStackTrace(); } Runnable b = new CMReplace2("B", cmp); new Thread(b).start(); } }
Output :
A maps the element {3=C, 2=B, 1=A, 4=D} Size of Map = 4 Key set of map = [3, 2, 1, 4] Previously associated value of '2' was : 'null' Previously associated value of '2' was : 'E' Previously associated value of '5' was : 'D' Set of key = [3, 2, 1, 4] Size of Map = 4 |
[ 0 ] Comments