Here you can find the source of putIfAbsent(Map
Parameter | Description |
---|---|
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; //License from project: Apache License import java.util.Map; public class Main { /**/* w w w . j a v a 2 s . c o m*/ * Backwards compatible {@link Map#putIfAbsent} to support < Android API 24. * * If the specified key is not already associated with a value (or is mapped * to {@code null}) associates it with the given value and returns * {@code null}, else returns the current value. * * @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 * {@code null} if there was no mapping for the key. */ public static <K, V> V putIfAbsent(Map<K, V> map, K key, V value) { V val = map.get(key); if (val == null) { val = map.put(key, value); } return val; } }