Java Map Copy copyValueIfExist(Map source, Map target, K key)

Here you can find the source of copyValueIfExist(Map source, Map target, K key)

Description

Copy value from the source map to the target map only if the value exists in the source map.

License

LGPL

Parameter

Parameter Description
K the key type
V the value type
source the source map
target the target map
key the key to copy

Return

true if exists and copied

Declaration

public static <K, V> boolean copyValueIfExist(Map<K, V> source, Map<K, V> target, K key) 

Method Source Code

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

import java.util.Map;

public class Main {
    /**//w  ww.j  a va  2 s . com
     * Copy value from the source map to the target map only if the value exists in the source map.
     * 
     * @param <K>
     *            the key type
     * @param <V>
     *            the value type
     * @param source
     *            the source map
     * @param target
     *            the target map
     * @param key
     *            the key to copy
     * @return <code>true</code> if exists and copied
     */
    public static <K, V> boolean copyValueIfExist(Map<K, V> source, Map<K, V> target, K key) {
        V v = source.get(key);
        if (v != null) {
            target.put(key, v);
            return true;
        }
        return false;
    }
}

Related

  1. copySafelyToObjectToObjectMap(java.util.Map map)
  2. copyStringMap(Map initParams)
  3. copyUntilFull(final Map source, final Map dest1, Map overflow, final int dest1Capacity)
  4. copyValue(Map source, Map target, K key)
  5. copyValueIfExist(Map source, K sourceKey, Map target, K targetKey)
  6. deepCopy(Map map)
  7. deepCopy(Map> context)
  8. deepCopyMap(Map map)