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

Backwards compatible Map#putIfAbsent to support < Android API 24.

License

Apache License

Parameter

Parameter Description
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.

Declaration

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

Method Source Code


//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 &lt; 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;
    }
}

Related

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