Java ConcurrentMap's remove(k,v) method.

Java ConcurrentMap's remove(k,v) method.


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

This section will illustrate you how can remove(k,v) method be implemented in ConcurrentMap interface in java. boolean remove(Object key, Object value) This method removes the mapping for a key if presently mapped to given value.

Java ConcurrentMap's remove(k,v) method.

This section will illustrate you how can remove(k,v) method be implemented in ConcurrentMap interface in java.

Syntax

boolean remove(Object key, Object value)

This method removes the mapping for a key if presently mapped to given value.

The basic work of this method is to perform the remove operation. Internally this method first do test whether the map contains a mapping for a key and then check for the value whether it is associated with the specified key if, it is true then the remove method removes that specified key association and returns 'true' otherwise returns 'false'.

Parameter description

key : It is the key with which the specified value is associated.

value : It is the value expected to be associated with the specified key.

Example of remove(k,v) method

In this example we will show you how does remove(k,v) method work in ConcurrentMap interface. This example will help you to understand how can you remove the association of a value with the specified key from the map. 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 CMRemove1 implements Runnable {
  ConcurrentMap cmp;
  String name;
  public CMRemove1(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(2, "B");
    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);
    try {
      Thread.sleep(400);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
class CMRemove2 implements Runnable {
  String name;
  ConcurrentMap cmp;
  public CMRemove2(String name, ConcurrentMap cmp) {
    this.cmp = cmp;
    this.name = name;
  }
  public void run() {
    try {
      boolean bol = cmp.remove(2, "B");
      System.out
          .println(name + " deleted the given association : " + bol);
      Thread.sleep(1500);
      System.out.println(name + " deleted the association (2,''B'')then "
          + "remaining association of map = " + cmp);
      System.out.println("Remaining Set of key = " + cmp.keySet());
      System.out.println("Remaining size of Map = " + cmp.size());
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
public class CMRemove {
  public static void main(String args[]) {
    ConcurrentMap cmp = 
      new ConcurrentHashMap();
    Runnable a = new CMRemove1("A", cmp);
    new Thread(a).start();
    try {
      Thread.sleep(400);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    Runnable b = new CMRemove2("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]

B deleted the given association : true

B deleted the association (2,''B'') then remaining association of map =
{3=C, 1=A, 4=D}

Remaining Set of key = [3, 1, 4]

Remaining size of Map = 3

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics