Java tutorial
//package com.java2s; //License from project: LGPL import java.util.Map; import java.util.Set; public class Main { /** * Copy values from the source map to the target map if the value exists in the source map. * * @param <K> * the key type * @param <V> * the value type * @param source * the source * @param target * the target * @param keys * the keys to copy */ public static <K, V> void copyValuesIfExist(Map<K, V> source, Map<K, V> target, Set<K> keys) { for (K key : keys) { copyValueIfExist(source, target, key); } } /** * 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; } }