Java ConcurrentMap's putIfAbsent(key,value) method.

Java ConcurrentMap's putIfAbsent(key,value) method.


Posted in : Core Java Posted on : March 10, 2011 at 7:38 PM Comments : [ 0 ]

In this section we will discuss how can putIfAbsent() method be implemented in ConcurrentMap interface in java. V putIfAbsent(K key,V value) This method associates a given value with key, when a particular key is not associated early with the key.

Java ConcurrentMap's putIfAbsent(key,value) method.

In this section we will discuss how can putIfAbsent() method be implemented in ConcurrentMap interface in java.

Syntax

V putIfAbsent(K key,V value)

This method associates a given value with key, when a particular key is not associated early with the key.

The basic work of this method is to perform read/write operation. Internally this method first do the read or search operation for a given value which you want to associate with the specified key and then it performs the write operation if the given value is not associated with the specified key. It returns the 'value' which had been associated early with the particular 'key' otherwise, returns 'null' if the mapping does not exist for the key. The returning of 'null' by this method can also mean that there is a null association for the key in map.

Parameter description

key : It takes a key into which you want to associate a value.

value : It takes a value which you want to associate with a specified key.

Example putIfAbsent() method 

In this example we will show you how does putIfAbsent(k,v) method work in ConcurrentMap interface. This example will help you to understand how can you associates a value with the specified key. Through this example we will show you what this method returns in different conditions.

Example :

package devmanuals.com;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Set;
class CMPutIfAbsent1 implements Runnable {
  String name;
  ConcurrentMap cm;
  public CMPutIfAbsent1(ConcurrentMap cm, String name) {
    this.name = name;
    this.cm = cm;
  }
  public void run() {
    try {
      cm.put(1, "A");
      cm.put(2, "B");
      cm.put(3, "C");
      cm.put(4, "D");
      cm.put(5, "E");
      System.out.println(name + " maps the element : " + cm);
      System.out.println(name + " represents the set of keys: "
          + cm.keySet());
      Thread.sleep(500);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
class CMPutIfAbsent2 implements Runnable {
  String name;
  ConcurrentMap cm;
  public CMPutIfAbsent2(ConcurrentMap cm, String name) {
    this.name = name;
    this.cm = cm;
  }
  public void run() {
    try {
      boolean j = cm.remove(3, "C");
      System.out.println(name + " removes the element : " + j);
      Thread.sleep(500);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
class CMPutIfAbsent3 implements Runnable {
  String name;
  ConcurrentMap cm;
  public CMPutIfAbsent3(ConcurrentMap cm, String name) {
    this.name = name;
    this.cm = cm;
  }
  public void run() {
    try {
      Set s = cm.keySet();
      System.out.println(name + " represents " +
          "the set of keys : " + s);
      // here the implementation of this method 
      //will return the previous value associated with key '4'
      String st = cm.putIfAbsent(4, "D");
      System.out.println(name+" returns the previous value : "+st);
      // here the implementation of this method 
      //will return the 'null' value associated with key '4'
      st = cm.putIfAbsent(3, "C");
      System.out.println(name+" returns : "+st);
      Thread.sleep(500);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
public class CMPutIfAbsent {
  public static void main(String[] args) {
  ConcurrentMap cm = 
          new ConcurrentHashMap();
    Runnable a = new CMPutIfAbsent1(cm, "A");
    Runnable b = new CMPutIfAbsent2(cm, "B");
    Runnable c = new CMPutIfAbsent3(cm, "C");
    new Thread(a).start();
    try {
      Thread.sleep(300);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    new Thread(b).start();
    try {
      Thread.sleep(300);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    new Thread(c).start();
    try {
      Thread.sleep(300);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

Output :

A maps the element : {5=E, 4=D, 3=C, 2=B, 1=A}

A represents the set of keys: [5, 4, 3, 2, 1]

B removes the element : true

C represents the set of keys : [5, 4, 2, 1]

C returns the previous value : D

C returns : null

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics