Java8 Collectors.toMap的坑

按照常规思维,往一个map里put一个已经存在的key,会把原有的key对应的value值覆盖,然而通过一次线上问题,发现Java8中的Collectors.toMap反其道而行之,它默认给抛异常,抛异常...

 

线上业务代码出现Duplicate Key的异常,影响了业务逻辑,查看抛出异常部分的代码,类似以下写法:

Map<Integer, String> map = list.stream().collect(Collectors.toMap(Person::getId, Person::getName));

然后list里面有id相同的对象,结果转map的时候居然直接抛异常了。。查源码发现toMap方法默认使用了个throwingMerger

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);
}
 
 
private static <T> BinaryOperator<T> throwingMerger() {
    return (u,v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); };
}

那么这个throwingMerger是哪里用的呢?

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);
}

这里传进去的是HashMap,所以最终走的是HashMap的merge方法。merge方法里面有这么一段代码:

if (old != null) {
    V v;
    if (old.value != null)
        v = remappingFunction.apply(old.value, value);
    else
        v = value;
    if (v != null) {
        old.value = v;
        afterNodeAccess(old);
    }
    else
        removeNode(hash, key, null, false, true);
    return v;
}

相信只看变量名就能知道这段代码啥意思了。。如果要put的key已存在,那么就调用传进来的方法。而throwingMerger的做法就是抛了个异常。所以到这里就可以知道写的代码为什么呲了。。 

如果不想抛异常的话,自己传进去一个方法即可,上述代码可以改成:

Map<Integer, String> map = list.stream().collect(Collectors.toMap(Person::getId, Person::getName,(oldValue, newValue) -> newValue));

这样就做到了使用新的value替换原有value。

 其中Person::getId中::为方法引用 ,Collectors.toMap()源码中第一个参数为Function,

Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
                                    Function<? super T, ? extends U> valueMapper,
                                    BinaryOperator<U> mergeFunction)

而Function中实际调用的是

@FunctionalInterface
public interface Function<T, R> {

    R apply(T t);

为传入一个参数,返回结果,如果不想引用现有方法,那么可以自己用lamda来指定,这种指定也简单 ,如传入参数返回参数值+1,可以直接用lamda写成e->e+1,所以

Collectors.toMap()也可以变化成Map中存放当前Person对象,键为id

Map<Integer, String> map = list.stream().collect(Collectors.toMap(Person::getId, p->p,(oldValue, newValue) -> newValue));

 

posted @ 2020-05-18 13:52  关键步就几步  阅读(5122)  评论(0编辑  收藏  举报