Builds a map from two arrays. - Android java.util

Android examples for java.util:Map

Description

Builds a map from two arrays.

Demo Code

/*/*from  ww  w .  j a v  a 2  s.  c o m*/
 * --------------------
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License("CDDL") (the "License").  You may not use this file
 * except in compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://opensource.org/licenses/cddl1.php
 * See the License for the specific language governing permissions and limitations
 * under the License.
 *
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://opensource.org/licenses/cddl1.php.
 * If applicable, add the following below this CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * --------------------
 */
//package com.book2s;

import java.util.HashMap;

import java.util.Map;
import java.util.Properties;

public class Main {
    public static Map<String, String> newMap(Properties properties) {
        Map<String, String> rv = new HashMap<String, String>();
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            rv.put((String) entry.getKey(), (String) entry.getValue());
        }
        return rv;
    }

    public static <T, K> Map<T, K> newMap(T k0, K v0) {
        Map<T, K> map = new HashMap<T, K>();
        map.put(k0, v0);
        return map;
    }

    public static <T, K> Map<T, K> newMap(T k0, K v0, T k1, K v1) {
        Map<T, K> map = newMap(k0, v0);
        map.put(k1, v1);
        return map;
    }

    public static <T, K> Map<T, K> newMap(T k0, K v0, T k1, K v1, T k2, K v2) {
        Map<T, K> map = newMap(k0, v0, k1, v1);
        map.put(k2, v2);
        return map;
    }

    public static <T, K> Map<T, K> newMap(T k0, K v0, T k1, K v1, T k2,
            K v2, T k3, K v3) {
        Map<T, K> map = newMap(k0, v0, k1, v1, k2, v2);
        map.put(k3, v3);
        return map;
    }

    public static <T, K> Map<T, K> newMap(T k0, K v0, T k1, K v1, T k2,
            K v2, T k3, K v3, T k4, K v4) {
        Map<T, K> map = newMap(k0, v0, k1, v1, k2, v2, k3, v3);
        map.put(k4, v4);
        return map;
    }

    public static <T, K> Map<T, K> newMap(T k0, K v0, T k1, K v1, T k2,
            K v2, T k3, K v3, T k4, K v4, T k5, K v5) {
        Map<T, K> map = newMap(k0, v0, k1, v1, k2, v2, k3, v3, k4, v4);
        map.put(k5, v5);
        return map;
    }

    /**
     * Builds a map from two arrays.
     *
     * @param k
     *            Array of keys.
     * @param v
     *            Array of values.
     * @return a map based on the two arrays.
     */
    public static <T, K> Map<T, K> newMap(T[] k, K[] v) {
        // throw if there's invalid input..
        if (k.length != v.length) {
            throw new IllegalArgumentException();
        }
        Map<T, K> map = new HashMap<T, K>(k.length);
        for (int i = 0; i < k.length; i++) {
            T key = k[i];
            K value = v[i];
            map.put(key, value);
        }
        return map;
    }
}

Related Tutorials