Java tutorial
//package com.java2s; import java.util.*; public class Main { /** * Converts the given pair of arrays into a sorted 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 extends Comparable<? super K>, V> Map<K, V> asMapSorted(K[] keys, V[] values) { Map<K, V> map = new TreeMap<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 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 extends Comparable<? super K>, V> Map<K, V> asMapSorted(List<K> keys, List<V> values) { Map<K, V> map = new TreeMap<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; } }