Create Map from array of key and array of value - Android java.util

Android examples for java.util:Map

Description

Create Map from array of key and array of value

Demo Code


//package com.book2s;

import java.util.HashMap;

import java.util.Map;

public class Main {

    public static <K, V> Map<K, V> toMap(K[] keys, V[] values) {
        HashMap<K, V> map = new HashMap<K, V>();

        if (keys != null) {
            for (int i = 0; i < keys.length; i++) {
                map.put(keys[i],/*from  w  w  w . j  a v a2 s  .  c  om*/
                        values != null ? (i < values.length ? values[i]
                                : null) : null);
            }
        }

        return map;
    }
}

Related Tutorials