Java Map Put putIfAbsent(Map map, K key, V value)

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

Description

If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.

License

Open Source License

Parameter

Parameter Description
K The map key type
V The map value type
map the map to use
key key with which the specified value is to be associated
value value to be associated with the specified key

Return

the previous value associated with the specified key, or null if there was no mapping for the key. (A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.)

Declaration

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

Method Source Code

//package com.java2s;
/**/*ww w.  j av a  2  s . c  o  m*/
 * Oshi (https://github.com/oshi/oshi)
 *
 * Copyright (c) 2010 - 2017 The Oshi Project Team
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Maintainers:
 * dblock[at]dblock[dot]org
 * widdis[at]gmail[dot]com
 * enrico.bianchi[at]gmail[dot]com
 *
 * Contributors:
 * https://github.com/oshi/oshi/graphs/contributors
 */

import java.util.Map;

public class Main {
    /**
     * If the specified key is not already associated with a value (or is mapped
     * to null) associates it with the given value and returns null, else
     * returns the current value.
     *
     * @param <K>
     *            The map key type
     * @param <V>
     *            The map value type
     * @param map
     *            the map to use
     * @param key
     *            key with which the specified value is to be associated
     * @param value
     *            value to be associated with the specified key
     * @return the previous value associated with the specified key, or null if
     *         there was no mapping for the key. (A null return can also
     *         indicate that the map previously associated null with the key, if
     *         the implementation supports null values.)
     */
    public static <K, V> V putIfAbsent(Map<K, V> map, K key, V value) {
        synchronized (map) {
            V existingValue = map.get(key);
            if (existingValue != null) {
                return existingValue;
            }
            map.put(key, value);
            return null;
        }
    }
}

Related

  1. putDataInToMap(String className, String MethodName)
  2. putDeepValue(Map map, String keyPath, T v)
  3. putIfAbsent(final Map from, final Map to)
  4. putIfAbsent(Map from, Map to)
  5. putIfAbsent(Map map, K key, V value)
  6. putIfAbsent(Map map, K key, V value)
  7. putIfAbsent(Map map, S key, T val)
  8. putIfAbsent(Map map, String name, String value)
  9. putIfAbsentGet(Map map, K key, V value)