Example usage for java.util.concurrent ConcurrentMap replace

List of usage examples for java.util.concurrent ConcurrentMap replace

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentMap replace.

Prototype

V replace(K key, V value);

Source Link

Document

Replaces the entry for a key only if currently mapped to some value.

Usage

From source file:Main.java

public static void main(String[] args) {
    ConcurrentMap<String, String> cMap = new ConcurrentHashMap<>();
    cMap.put("A", "A");

    System.out.println("Concurrent Map: " + cMap);

    System.out.println(cMap.putIfAbsent("A", "1"));
    System.out.println(cMap.putIfAbsent("B", "B"));
    System.out.println(cMap.remove("A", "B"));
    System.out.println(cMap.replace("A", "B"));

    System.out.println("Concurrent Map: " + cMap);
}

From source file:Main.java

/**
 * Replaces the entry for a key only if currently mapped to some value.
 *
 * @param map   The map to be operated on.
 * @param key   The key with which the specified value is associated.
 * @param value The value to be associated with the specified key.
 * @param <K>   The class of keys in this map.
 * @param <V>   The class of values in the map.
 * @return      The previous value associated with the specified key, or null if there was no mapping for the key.
 *///from w w  w  .ja  va 2 s.co m
public static <K, V> V replace(ConcurrentMap<K, V> map, K key, V value) {
    if (map == null)
        return null;
    return map.replace(key, value);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap", expectedExceptions = NullPointerException.class)
public void replace_withNullKey(ConcurrentMap<Integer, Integer> map) {
    map.replace(null, 2);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap", expectedExceptions = NullPointerException.class)
public void replace_withNullValue(ConcurrentMap<Integer, Integer> map) {
    map.replace(1, null);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap", expectedExceptions = NullPointerException.class)
public void replace_withNullEntry(ConcurrentMap<Integer, Integer> map) {
    map.replace(null, null);
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap")
public void replace_whenEmpty(ConcurrentMap<Integer, Integer> map) {
    assertThat(map.replace(1, 2), is(nullValue()));
}

From source file:com.googlecode.concurrentlinkedhashmap.ConcurrentMapTest.java

@Test(dataProvider = "guardedMap")
public void replace_whenPopulated(ConcurrentMap<Integer, Integer> map) {
    map.put(1, 2);//from  w ww.j  av  a 2  s  .  co  m
    assertThat(map.replace(1, 3), is(2));
    assertThat(map.get(1), is(3));
    assertThat(map.size(), is(1));
}