CustomCollectors { public static <T, K, U, M extends Map<K,U>> Collector<T, ?, M> toCustomMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, Supplier<M> mapSupplier) { return Collectors.toMap(keyMapper, valueMapper, throwingMerger(), mapSupplier); } private  CustomCollectors(); static Collector<T, ?, Map<K,U>> toLinkedMap(Function<? super T, ? extends K> keyMapper,
                                          Function<? super T, ? extends U> valueMapper); static Collector<T, ?, M> toCustomMap(Function<? super T, ? extends K> keyMapper,
                                   Function<? super T, ? extends U> valueMapper,
                                   Supplier<M> mapSupplier);  }
@Test public void custom_map_collector_returns_map_from_given_supplier() { List<String> stringList = numberList(); Map<String, String> customMap = stringList.stream().collect(toCustomMap(identity(), identity(), CustomHashMap::new)); assertEquals(CustomHashMap.class, customMap.getClass()); }
@Test public void custom_map_collector_throws_exception_upon_duplicate_keys() { List<String> duplicates = Lists.newArrayList("same", "same"); thrown.expect(DuplicateKeyException.class); duplicates.stream().collect(toCustomMap(Function.identity(), Function.identity(), HashMap::new)); }