Java ConcurrentMap putIfAbsent(ConcurrentMap map, K key, V value)

Here you can find the source of putIfAbsent(ConcurrentMap map, K key, V value)

Description

Puts a value in the specified ConcurrentMap if the key is not yet present.

License

Apache License

Parameter

Parameter Description
K the type of the keys of the map
V the type of the values of the map
map the map to be modified
key the key of the value to be added
value the value to be added

Return

the value stored in the map after this operation

Declaration

public static <K, V> V putIfAbsent(ConcurrentMap<K, V> map, K key, V value) 

Method Source Code


//package com.java2s;
/*/*from w w  w .  j  ava2  s .  com*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.concurrent.ConcurrentMap;

public class Main {
    /**
     * <p>
     * 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;
    }
}

Related

  1. getTypeFromKind(String kind)
  2. is(Class clazz, Collection> clazzes, ConcurrentMap, Boolean> cache)
  3. isClassClass(Class clazz)
  4. putIfAbsent(ConcurrentMap map, K key, V value)
  5. putIfAbsent(ConcurrentMap map, K key, V value)
  6. putIfAbsent(ConcurrentMap target, K key, V value)
  7. putIfAbsent(final ConcurrentMap map, final K key, final V value)
  8. putIfAbsentAndGet(ConcurrentMap map, K key, V newValue)
  9. remove(ConcurrentMap map, K key, V value)