combine two array into a Map - Java java.lang

Java examples for java.lang:int Array

Description

combine two array into a Map

Demo Code

/*//  www.  j a v a2s.  c  om
 * MoXie (SysTem128@GMail.Com) 2009-7-27 11:04:53
 * 
 * Copyright © 2008-2009 Zoeey.Org
 * Code license: GNU Lesser General Public License Version 3
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 */
//package com.java2s;

import java.util.LinkedHashMap;

import java.util.Map;

public class Main {

    public static <T, V> Map<T, V> combine(T[] keys, V[] values) {
        Map<T, V> map = new LinkedHashMap<T, V>();
        if (keys != null && keys.length > 0) {
            int vsize = values == null ? 0 : values.length;
            for (int i = 0; i < keys.length; i++) {
                if (i >= vsize) {
                    map.put(keys[i], null);
                } else {
                    map.put(keys[i], values[i]);
                }
            }
        }
        return map;
    }
}

Related Tutorials