Java 8 Collectors.toMap SortedMap
2022-08-31 12:37:36
我正在使用Java 8 lambdas,并希望使用返回.我能想到的最好的方法是用假人调用以下方法,并等于.Collectors
toMap
SortedMap
Collectors
toMap
mergeFunction
mapSupplier
TreeMap::new
public static <T, K, U, M extends Map<K, U>>
Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapSupplier) {
BiConsumer<M, T> accumulator = (map, element) -> map.merge(keyMapper.apply(element),
valueMapper.apply(element), mergeFunction);
return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_ID);
}
我不想像我想要的那样传递合并函数,就像基本实现一样,如下所示:throwingMerger()
toMap
public static <T, K, U>
Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper) {
return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new);
}
使用返回 ?Collectors
SortedMap