Example usage for java.util.concurrent ConcurrentMap putIfAbsent

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

Introduction

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

Prototype

V putIfAbsent(K key, V value);

Source Link

Document

If the specified key is not already associated with a value, associates it with the given 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:org.slc.sli.dal.convert.SubDocAccessor.java

public static void main(String[] args) {
    PrintStream output = System.out;
    SubDocAccessor accessor = new SubDocAccessor(null, null, null);
    ConcurrentMap<String, List<String>> collections = new ConcurrentHashMap<String, List<String>>();
    for (Entry<String, Location> entry : accessor.locations.entrySet()) {
        String type = entry.getKey();
        String collectionName = entry.getValue().collection;
        collections.putIfAbsent(collectionName, new ArrayList<String>());
        collections.get(collectionName).add(type);
    }/*  w w w. ja v  a2 s. c om*/
    for (Entry<String, List<String>> entry : collections.entrySet()) {
        output.println(entry.getKey() + ": " + StringUtils.join(entry.getValue().toArray(), ", "));
    }
}

From source file:Main.java

public static void addLong(ConcurrentMap<String, AtomicLong> map, String key, long delta) {
    map.putIfAbsent(key, new AtomicLong(0));
    map.get(key).getAndAdd(delta);/*  w w w  .jav  a2s  .c o m*/
}

From source file:Main.java

public static <K, V> V putIfAbsent(ConcurrentMap<K, V> map, K key, V value) {
    V current = map.putIfAbsent(key, value);
    return current != null ? current : value;
}

From source file:Main.java

public static void setLong(ConcurrentMap<String, AtomicLong> map, String key, long newValue) {
    map.putIfAbsent(key, new AtomicLong(0));
    map.get(key).getAndSet(newValue);// www .  ja  v  a2 s.  c o  m
}

From source file:Main.java

public static <K, V> V putIfAbsent(final ConcurrentMap<K, V> map, final K key, final V value) {
    final V exists = map.putIfAbsent(key, value);
    if (exists != null) {
        return exists;
    }//from  w  w  w  . ja  v a2s.  c  o  m
    return value;
}

From source file:Main.java

/**
 * If the specified key is not already associated with a value, associate it with the given value.
 *
 * @param map   The map to be operated on.
 * @param key   The key with which the specified value is to be 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  www . j  a v  a  2 s  .  com
public static <K, V> V putIfAbsent(ConcurrentMap<K, V> map, K key, V value) {
    if (map == null)
        return null;
    return map.putIfAbsent(key, value);
}

From source file:Main.java

/**
 * If map already has a value associated with the key it adds values to that
 * value, otherwise it will put values to the map.  Do not reuse values.
 *
 * @param key    Key under which we are adding values
 * @param values Values we want to add/*www.  j ava 2s . c o  m*/
 * @param map    Map which we are adding values to
 * @param <K> Key
 * @param <V> Value
 * @param <C> Collection
 * @return New value associated with the key
 */
public static <K, V, C extends Collection<V>> C addConcurrent(K key, C values, ConcurrentMap<K, C> map) {
    C currentValues = map.get(key);
    if (currentValues == null) {
        currentValues = map.putIfAbsent(key, values);
        if (currentValues == null) {
            return values;
        }
    }
    synchronized (currentValues) {
        currentValues.addAll(values);
    }
    return currentValues;
}

From source file:Main.java

/**
 * <p>//from  w  w  w. ja  v  a 2  s.co  m
 * Puts a value in the specified {@code ConcurrentMap} if the key is not yet
 * present. This method works similar to the {@code putIfAbsent()} method of
 * the {@code ConcurrentMap} interface, but the value returned is different.
 * Basically, this method is equivalent to the following code fragment:
 *
 * <pre>
 * if (!map.containsKey(key)) {
 *     map.put(key, value);
 *     return value;
 * } else {
 *     return map.get(key);
 * }
 * </pre>
 *
 * except that the action is performed atomically. So this method always
 * returns the value which is stored in the map.
 * </p>
 * <p>
 * This method is <b>null</b>-safe: It accepts a <b>null</b> map as input
 * without throwing an exception. In this case the return value is
 * <b>null</b>, too.
 * </p>
 *
 * @param <K> the type of the keys of the map
 * @param <V> the type of the values of the map
 * @param map the map to be modified
 * @param key the key of the value to be added
 * @param value the value to be added
 * @return the value stored in the map after this operation
 */
public static <K, V> V putIfAbsent(ConcurrentMap<K, V> map, K key, V value) {
    if (map == null) {
        return null;
    }

    V result = map.putIfAbsent(key, value);
    return result != null ? result : value;
}

From source file:com.sm.store.cluster.Utils.java

public static String validateServer(List<ClusterNodes> nodes) {
    StringBuilder sb = new StringBuilder();
    ConcurrentMap<String, Short> map = new ConcurrentHashMap<String, Short>(nodes.size());
    for (ClusterNodes node : nodes) {
        for (int i = 0; i < node.getServerArray().length; i++) {
            if (map.putIfAbsent(node.getServerArray()[i], node.getId()) != null)
                sb.append(" id =" + node.getId());
        }//from  ww w .  j  a  v  a  2 s  .  c  om
    }
    return sb.toString();
}