Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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) {
        V v = source.get(key);
        if (v != null) {
            target.put(key, v);
            return true;
        }
        return false;
    }
}