Java Map Copy copyValueIfExist(Map source, K sourceKey, Map target, K targetKey)

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

Description

Copy value if it exists (is not null) from the source map.

License

LGPL

Parameter

Parameter Description
K the key type
V the value type
source the source
sourceKey the source key
target the target
targetKey the target key

Return

true, if successful

Declaration

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

Method Source Code

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

import java.util.Map;

public class Main {
    /**//from w  w  w  .  j  a v  a2s.  c  om
     * 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) {
        return copyValueIfExist(source, key, target, key);
    }

    /**
     * Copy value if it exists (is not <code>null</code>) from the source map.
     *
     * @param <K>
     *            the key type
     * @param <V>
     *            the value type
     * @param source
     *            the source
     * @param sourceKey
     *            the source key
     * @param target
     *            the target
     * @param targetKey
     *            the target key
     * @return true, if successful
     */
    public static <K, V> boolean copyValueIfExist(Map<K, V> source, K sourceKey, Map<K, V> target, K targetKey) {
        V v = source.get(sourceKey);
        if (v != null) {
            target.put(targetKey, v);
            return true;
        }
        return false;
    }
}

Related

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