Java Map Put putIfNotExists(final K key, final V value, final Map map)

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

Description

Puts a value with a key into a map if the key is previously unset.

License

Apache License

Parameter

Parameter Description
key a parameter
value a parameter
map a parameter

Return

true if the item was successfully put, otherwise false

Declaration

public static <K, V extends Object> boolean putIfNotExists(final K key, final V value, final Map<K, V> map) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Map;

public class Main {
    /**//from  w  w w  .  j a  va  2  s  . c  o  m
     * Puts a value with a key into a map if the key is previously unset.
     * 
     * @param key
     * @param value
     * @param map
     * @return <code>true</code> if the item was successfully put, otherwise
     *         <code>false</code>
     */
    public static <K, V extends Object> boolean putIfNotExists(final K key, final V value, final Map<K, V> map) {
        if (!map.containsKey(key)) {
            map.put(key, value);
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. putIfAbsent(Map map, S key, T val)
  2. putIfAbsent(Map map, String name, String value)
  3. putIfAbsentGet(Map map, K key, V value)
  4. putIfNonNull(Map map, K key, V value)
  5. putIfNotExist(Map map, K key, V value)
  6. putIfNotExists(Map map, K key, V value)
  7. putIfNotNull(final Map map, final String name, final String value)
  8. putIfNotNull(Map map, K key, V value)
  9. putIfNotNull(Map> params, String key, T value)