Java tutorial
//package com.java2s; import java.util.*; public class Main { /** * Converts the given pair of arrays into a map that maps each keys[i] to * the corresponding values[i]. * @return the map (empty map if keys == null or values == null) */ public static <K, V> Map<K, V> asMap(K[] keys, V[] values) { Map<K, V> map = new LinkedHashMap<K, V>(); if (keys == null || values == null) { return map; } for (int i = 0, len = Math.min(keys.length, values.length); i < len; i++) { map.put(keys[i], values[i]); } return map; } /** * Converts the given pair of lists into a map that maps each keys[i] to * the corresponding values[i]. * @return the map (empty map if keys == null or values == null) */ public static <K, V> Map<K, V> asMap(List<K> keys, List<V> values) { Map<K, V> map = new LinkedHashMap<K, V>(); if (keys == null || values == null) { return map; } for (int i = 0, len = Math.min(keys.size(), values.size()); i < len; i++) { map.put(keys.get(i), values.get(i)); } return map; } }