/** * Generates a map from an array an entries. Entries are defined in the API * as a class which consists of a {@code key} and {@code value} with a name * typically in the form of {@code Key_ValueMapEntry}, represented here as * {@code T}. The generated map must have same type {@code K} as the {@code * key} within {@code Key_ValueMapEntry}. The same applies for {@code V} and * {@code value} within {@code Key_ValueMapEntry}. If * {@code entryArray == null}, an emtpy map is returned. * * @param <K> the type of the entry key * @param <V> the type of the entry value * @param <T> the map entry type * @param entryArray the array of entries constituting a map as represented in * the API * @return a map of type {@code K} and {@code V} representing the entry array * @throws IllegalArgumentException if {@code entryArray} is not compatible * with {@code Map<K, V>}. */ public static <K, V, T> Map<K, V> toMap(T[] entryArray) throws IllegalArgumentException { if (entryArray == null) { return com.google.common.collect.Maps.<K, V>newHashMap(); } return toMap(Arrays.asList(entryArray)); }
/** * Generates a map from an array an entries. Entries are defined in the API * as a class which consists of a {@code key} and {@code value} with a name * typically in the form of {@code Key_ValueMapEntry}, represented here as * {@code T}. The generated map must have same type {@code K} as the {@code * key} within {@code Key_ValueMapEntry}. The same applies for {@code V} and * {@code value} within {@code Key_ValueMapEntry}. If * {@code entryArray == null}, an emtpy map is returned. * * @param <K> the type of the entry key * @param <V> the type of the entry value * @param <T> the map entry type * @param entryArray the array of entries constituting a map as represented in * the API * @return a map of type {@code K} and {@code V} representing the entry array * @throws IllegalArgumentException if {@code entryArray} is not compatible * with {@code Map<K, V>}. */ public static <K, V, T> Map<K, V> toMap(T[] entryArray) throws IllegalArgumentException { if (entryArray == null) { return com.google.common.collect.Maps.<K, V>newHashMap(); } return toMap(Arrays.asList(entryArray)); }
@Test public void testToMap_array() { Map<String, String> map = Maps.<String, String, Entry>toMap(entriesList.toArray(new Entry[entriesList.size()])); assertEquals(entriesMap, map); }
@Test public void testToMap_list() { Map<String, String> map = Maps.<String, String, Entry>toMap(entriesList); assertEquals(entriesMap, map); }