Here you can find the source of putIfAbsent(Map
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 |
public static <K, V> V putIfAbsent(Map<K, V> map, K key, V value)
//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; } } }