Java tutorial
//package com.java2s; /***************************************************************************************** * *** BEGIN LICENSE BLOCK ***** * * Version: MPL 2.0 * * echocat Jomon, Copyright (c) 2012-2014 echocat * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * *** END LICENSE BLOCK ***** ****************************************************************************************/ import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.*; public class Main { /** * Returns a {@link LinkedHashMap} * with the mappings <code>a[0] => a[1], a[2] => a[3], ...</code>. * @param a the elements to construct a {@link Map} from. * @return a {@link Map} constructed of the specified elements. */ @Nonnull public static <K, V> Map<K, V> asMap(@Nullable Object... a) { return putAll(new LinkedHashMap<K, V>(), a); } /** * Returns a the given map enriched * with the mappings <code>a[0] => a[1], a[2] => a[3], ...</code>. * @param a the elements to construct a {@link Map} from. * @return a {@link Map} constructed of the specified elements. */ @Nonnull public static <K, V> Map<K, V> putAll(@Nonnull Map<K, V> original, @Nullable Object... a) { if (a != null) { final int length = a.length; if (length % 2 == 1) { throw new IllegalArgumentException("You must provide an even number of arguments."); } for (int i = 0; i < length; i += 2) { // noinspection unchecked original.put((K) a[i], (V) a[i + 1]); } } return original; } @Nonnull public static <K, V> Map<K, V> putAll(@Nonnull Map<K, V> original, @Nullable Map<K, V> other) { if (other != null) { original.putAll(other); } return original; } }