Java Map Put putAllIfAbsent(final Map target, final Map source)

Here you can find the source of putAllIfAbsent(final Map target, final Map source)

Description

Puts entries from the source map into the target map, but without overriding any existing entry in target map, i.e.

License

Open Source License

Parameter

Parameter Description
K Key
V Value
target The target map where to put new entries.
source The source map from which read the entries.

Declaration

public static <K, V> void putAllIfAbsent(final Map<K, V> target, final Map<K, V> source) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Map;

public class Main {
    /**/*from   w ww  . j a va2  s.  c o m*/
     * Puts entries from the {@code source} map into the {@code target} map, but without overriding
     * any existing entry in {@code target} map, i.e. put only if the key does not exist in the
     * {@code target} map.
     *
     * @param <K>    Key
     * @param <V>    Value
     * @param target The target map where to put new entries.
     * @param source The source map from which read the entries.
     */
    public static <K, V> void putAllIfAbsent(final Map<K, V> target, final Map<K, V> source) {
        source.entrySet().stream().filter(entry -> !target.containsKey(entry.getKey()))
                .forEach(entry -> target.put(entry.getKey(), entry.getValue()));
    }
}

Related

  1. putAll(Map dest, Collection src, V value)
  2. putAll(Map map, Map otherMap)
  3. putAll(Map map, Object... keysAndValues)
  4. putAll(Map model, String prefix, Map subModel)
  5. putAll(Map map, String prefix, Map values)
  6. putAllIfNew(Map dest, Map source)
  7. putAllIfNotNull(final Map map, final Map m)
  8. putAllNewInMap(Map original, Map newStuff)
  9. putAllNonNullValues(Map source, Map target)