@Test(expected = IllegalArgumentException.class) public void shouldRejectOddNumberOfArguments(){ Collections.map("hi"); }
public static <K, V> Map<K, V> map(Object... keysAndValues) { if (keysAndValues.length % 2 != 0) { throw new IllegalArgumentException("number of arguments must be even"); } Map<K, V> map = new HashMap<K, V>(Math.max(keysAndValues.length, 16)); for (int i = 0; i < keysAndValues.length;) { map.put((K) keysAndValues[i++], (V) keysAndValues[i++]); } return map; }
Collections { public static <K, V> Map<K, V> map(Object... keysAndValues) { if (keysAndValues.length % 2 != 0) { throw new IllegalArgumentException("number of arguments must be even"); } Map<K, V> map = new HashMap<K, V>(Math.max(keysAndValues.length, 16)); for (int i = 0; i < keysAndValues.length;) { map.put((K) keysAndValues[i++], (V) keysAndValues[i++]); } return map; } }
Collections { public static <K, V> Map<K, V> map(Object... keysAndValues) { if (keysAndValues.length % 2 != 0) { throw new IllegalArgumentException("number of arguments must be even"); } Map<K, V> map = new HashMap<K, V>(Math.max(keysAndValues.length, 16)); for (int i = 0; i < keysAndValues.length;) { map.put((K) keysAndValues[i++], (V) keysAndValues[i++]); } return map; } private  Collections(); }
Collections { public static <K, V> Map<K, V> map(Object... keysAndValues) { if (keysAndValues.length % 2 != 0) { throw new IllegalArgumentException("number of arguments must be even"); } Map<K, V> map = new HashMap<K, V>(Math.max(keysAndValues.length, 16)); for (int i = 0; i < keysAndValues.length;) { map.put((K) keysAndValues[i++], (V) keysAndValues[i++]); } return map; } private  Collections(); static T[] arr(T... values); static T[] array(T... values); static Set<T> set(T... values); static Map<K, V> map(Object... keysAndValues); static List<T> li(T... values); static List<T> list(T... values); }
Collections { public static <K, V> Map<K, V> map(Object... keysAndValues) { if (keysAndValues.length % 2 != 0) { throw new IllegalArgumentException("number of arguments must be even"); } Map<K, V> map = new HashMap<K, V>(Math.max(keysAndValues.length, 16)); for (int i = 0; i < keysAndValues.length;) { map.put((K) keysAndValues[i++], (V) keysAndValues[i++]); } return map; } private  Collections(); static T[] arr(T... values); static T[] array(T... values); static Set<T> set(T... values); static Map<K, V> map(Object... keysAndValues); static List<T> li(T... values); static List<T> list(T... values);  }