CollectionUtils { public static <K, V> Map<K, V> combineLists(List<? extends K> keys, List<? extends V> values) { if (keys.size() != values.size()) { throw new IllegalArgumentException("Cannot combine lists with dissimilar sizes"); } if (keys.isEmpty() && values.isEmpty()) { return Collections.emptyMap(); } int size = values.size(); Map<K, V> map = new LinkedHashMap<>(size); for (int i = 0; i < size; i++) { map.put(keys.get(i), values.get(i)); } return map; } private  CollectionUtils(); static Map<K, V> combineLists(List<? extends K> keys, List<? extends V> values);  }
@Test public void combineNormalLists() throws Exception { List<String> firstList = Arrays.asList("1", "2", "3"); List<Integer> secondList = Arrays.asList(4, 5, 6); Map<String, Integer> expected = new LinkedHashMap<>(); expected.put("1", 4); expected.put("2", 5); expected.put("3", 6); assertEquals(expected, combineLists(firstList, secondList)); }
@Test(expected = IllegalArgumentException.class) public void exceptionOnCombineListsWithDissimilarSize() throws Exception { List<String> firstList = Arrays.asList("1", "2", "3", "4"); List<Integer> secondList = Arrays.asList(4, 5, 6); combineLists(firstList, secondList); }
@Test public void combineEmptyLists() throws Exception { List<?> firstList = Collections.emptyList(); List<?> secondList = Collections.emptyList(); assertEquals(Collections.emptyMap(), combineLists(firstList, secondList)); }