Android examples for java.util:Map
Returns a the given map enriched with the mappings a[0] => a[1], a[2] => a[3], ....
/***************************************************************************************** * *** BEGIN LICENSE BLOCK *****/* ww w. j a va 2 s. co m*/ * * Version: MPL 2.0 * * echocat Locela - API for Java, Copyright (c) 2014-2016 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.Nonnegative; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.*; import static java.util.Collections.*; import static org.echocat.locela.api.java.utils.ResourceUtils.closeQuietlyIfAutoCloseable; public class Main{ /** * 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 immutable {@link Map} constructed of the specified elements. */ @Nonnull public static <K, V> Map<K, V> putAllAndMakeImmutable( @Nonnull Map<K, V> original, @Nullable Object... a) { return asImmutableMap(putAll(original, a)); } /** * 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 immutable {@link Map} constructed of the specified elements. */ @Nonnull public static <K, V> Map<K, V> asImmutableMap(@Nullable Object... a) { return putAllAndMakeImmutable(new LinkedHashMap<K, V>(), a); } @Nonnull public static <K, V> Map<K, V> asImmutableMap(@Nullable Map<K, V> map) { return map != null ? unmodifiableMap(map) : Collections .<K, V> emptyMap(); } /** * 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; } }