Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.HashMap;

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**
     * Copies the entries of the given source into the given target.
     *
     * @param <TKey> The type of the keys of the given target map.
     * @param <TValue> Th type of the values of the given target map.
     * @param target The given target to copy the entries to.
     * @param source The given map to copy the entries from.
     * @note In case the key already exists in the given target, the value is
     * overwritten.
     */
    public static <TKey, TValue> void putAll(final Map<TKey, TValue> target,
            final Map<? extends TKey, ? extends TValue> source) {
        if (target != null && source != null) {
            putAll(target, source.entrySet());
        }
    }

    /**
     * Copies the entries of the given source into a new HashMap.
     *
     * @param <TKey> The type of the keys of the given target map.
     * @param <TValue> The type of the values of the given.
     * @param source The given iterable of entries, to copy the entries from.
     * @return A hashmap that contains all the entries of the given iterable.
     * @note In multiple entries occur with the same value. The value of the
     * last tuple is picked.
     */
    public static <TKey, TValue> HashMap<TKey, TValue> putAll(
            final Iterable<? extends Entry<? extends TKey, ? extends TValue>> source) {
        HashMap<TKey, TValue> map = new HashMap<>();
        putAll(map, source);
        return map;
    }

    /**
     * Copies the entries of the given source into the given target.
     *
     * @param <TKey> The type of the keys of the given target map.
     * @param <TValue> The type of the values of the given
     * @param target The given to target to copy the entries to.
     * @param source The given iterable of entries, to copy the entries from.
     * @note In case the key already exists in the given target, the value is
     * overwritten.
     */
    public static <TKey, TValue> void putAll(final Map<TKey, TValue> target,
            final Iterable<? extends Entry<? extends TKey, ? extends TValue>> source) {
        if (target != null && source != null) {
            for (Entry<? extends TKey, ? extends TValue> entry : source) {
                target.put(entry.getKey(), entry.getValue());
            }
        }
    }
}