Creates and returns a new map from the two-dimensional array. - Android java.util

Android examples for java.util:Map

Description

Creates and returns a new map from the two-dimensional array.

Demo Code

/*//from   w  w  w . j av  a2 s. c o  m
 * Copyright 2012 Marco Soeima
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
//package com.book2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Creates and returns a new map from the two-dimensional <code>array</code>.
     *
     * @param   <K>
     * @param   <V>
     * @param   array  The two-dimensional array from which a map is created.
     *
     * @return  A new map.
     */
    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> asMap(Object[][] array) {
        Map<K, V> map = new HashMap<K, V>();

        for (int i = 0; i < array.length; ++i) {
            map.put((K) array[i][0], (V) array[i][1]);
        }

        return map;
    }
}

Related Tutorials