Returns a LinkedHashMap with the mappings a[0] => a[1], a[2] => a[3], .... - Android java.util

Android examples for java.util:Map

Description

Returns a LinkedHashMap with the mappings a[0] => a[1], a[2] => a[3], ....

Demo Code

/*****************************************************************************************
 * *** BEGIN LICENSE BLOCK *****//from ww w.j  a va 2 s  . c  o  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{
    public static void main(String[] argv){
        Object a = "book2s.com";
        System.out.println(asImmutableMap(a));
    }
    /**
     * Returns a {@link LinkedHashMap}
     * with the mappings <code>a[0] =&gt; a[1], a[2] =&gt; 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] =&gt; a[1], a[2] =&gt; 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 the given map enriched
     * with the mappings <code>a[0] =&gt; a[1], a[2] =&gt; 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;
    }
}

Related Tutorials