java stream collect toMap nullPointException异常

目录

记java stream toMap的坑

1、先介绍一下Collectors.toMap()这个方法

其中的参数意义:

2、模拟一下调用toMap出异常的情况

3、剖其根本,找出原因

3.1、首先我们可以看看方法注释是否有做说明

3.2、然后我们可以去查看java api文档,看看是否有做说明

3.3、查看merge源码

4、推测value不能为null的原因

4.1、ConcurrentHashMap的putVal方法和merge方法

4.2、ConcurrentHashMap为什么不允许key和value为null呢

5、怎么避免在使用toMap的时候抛空指针异常

5.1、若数据是从DB中查出来的对象列表

5.2、若数据不是从DB从查询,而是业务逻辑中的数据,或者DB中确实有null属性值存在


记java stream toMap的坑

    java8中提供了Stream API这种非常方便的api流处理类,里边丰富的api可以使代码中的数据像在使用sql一样进行过滤等的操作。其中stream.collect(Collectors.toMap) 用来将一个集合构建一个映射关系map也是经常用来使用;

    例如:

        Map<String, String> map = list.stream().collect(Collectors.toMap(
                Node::getId,
                Node::getName,
                (n1, n2) -> n1
        ));

可以将一个list中的元素转换成key为id,value为name的map键值对对象;

但是可能会出现一个异常,在构建时明明list中元素都不为null,却出现了null空指针异常。

Exception in thread "main" java.lang.NullPointerException
	at java.util.HashMap.merge(HashMap.java:1225)
	at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
	at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
	at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)

解决方案可直接看 5、怎么避免在使用toMap的时候抛空指针异常

1、先介绍一下Collectors.toMap()这个方法

    public static <T, K, U>
    Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
                                    Function<? super T, ? extends U> valueMapper) {
        // 参数三(校验策略):当有重复key时,直接抛出异常
        return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new);
    }

    public static <T, K, U>
    Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
                                    Function<? super T, ? extends U> valueMapper,
                                    BinaryOperator<U> mergeFunction) {
        return toMap(keyMapper, valueMapper, mergeFunction, HashMap::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);
    }

我们的例子只有三个参数,所以调用的是第二个toMap,在其方法中再传递了一个HashMap来调用第三个toMap;

最终调用的都是有4个参数的toMap()方法

其中的参数意义:

  • Function<? super T, ? extends K> keyMapper:构建map的key
  • Function<? super T, ? extends U> valueMapper:构建map的value
  • BinaryOperator<U> mergeFunction:当有重复key时的合并策略
  • Supplier<M> mapSupplier:构建的map类型,默认为HashMap

2、模拟一下调用toMap出异常的情况

public class Note {

    public static void main(String[] args) {
        Node node1 = new Node("1","zhangsan");
        Node node2 = new Node("2","lisi");
        Node node3 = new Node("3",null);
        Node node4 = new Node("4","wanglaowu");
        List<Node> list = Arrays.asList(node1,node2,node3,node4);
        Map<String, String> map = list.stream().collect(Collectors.toMap(
                Node::getId,
                Node::getName,
                (n1, n2) -> n1
        ));
    }
}
class Node {
    private String id;
    private String name;
    public Node(String id, String name) {
        this.id = id;
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

如果使用以上代码来进行一个toMap构建键值对,咋一看貌似没有什么问题,list中的元素对象都不为null,不过是对象中的元素有部分为null,但是在HashMap中,key和value都是能为null的,为什么会出现空指针异常呢?

3、剖其根本,找出原因

首先我们可以通过异常堆栈信息定位到错误发生的地方是HashMap的merge()方法

为什么value为null时就直接抛空指针异常了呢?这与我们认识的HashMap可不一样呀!!!

而在其上一层的toMap()方法中,调用是Map实现类的merge方法进行一个KV的插入操作,故错误还是定位在merge()方法中

    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实现类的merge方法进行一个KV的插入操作
                = (map, element) -> map.merge(keyMapper.apply(element),
                                              valueMapper.apply(element), mergeFunction);
        return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_ID);
    }

3.1、首先我们可以看看方法注释是否有做说明

HashMap的实现中并没有给方法做注释,所以我们可以到父类Map中看其注释说明

    /**
     * If the specified key is not already associated with a value or is
     * associated with null, associates it with the given non-null value.
     * Otherwise, replaces the associated value with the results of the given
     * remapping function, or removes if the result is {@code null}. This
     * method may be of use when combining multiple mapped values for a key.
     * For example, to either create or append a {@code String msg} to a
     * value mapping:
     *
     * <pre> {@code
     * map.merge(key, msg, String::concat)
     * }</pre>
     *
     * <p>If the function returns {@code null} the mapping is removed.  If the
     * function itself throws an (unchecked) exception, the exception is
     * rethrown, and the current mapping is left unchanged.
     *
     * @implSpec
     * The default implementation is equivalent to performing the following
     * steps for this {@code map}, then returning the current value or
     * {@code null} if absent:
     *
     * <pre> {@code
     * V oldValue = map.get(key);
     * V newValue = (oldValue == null) ? value :
     *              remappingFunction.apply(oldValue, value);
     * if (newValue == null)
     *     map.remove(key);
     * else
     *     map.put(key, newValue);
     * }</pre>
     *
     * <p>The default implementation makes no guarantees about synchronization
     * or atomicity properties of this method. Any implementation providing
     * atomicity guarantees must override this method and document its
     * concurrency properties. In particular, all implementations of
     * subinterface {@link java.util.concurrent.ConcurrentMap} must document
     * whether the function is applied once atomically only if the value is not
     * present.
     *
     * @param key key with which the resulting value is to be associated
     * @param value the non-null value to be merged with the existing value
     *        associated with the key or, if no existing value or a null value
     *        is associated with the key, to be associated with the key
     * @param remappingFunction the function to recompute a value if present
     * @return the new value associated with the specified key, or null if no
     *         value is associated with the key
     * @throws UnsupportedOperationException if the {@code put} operation
     *         is not supported by this map
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
     * @throws ClassCastException if the class of the specified key or value
     *         prevents it from being stored in this map
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
     * @throws NullPointerException if the specified key is null and this map
     *         does not support null keys or the value or remappingFunction is
     *         null
     * @since 1.8
     */
    default V merge(K key, V value,
            BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
        Objects.requireNonNull(remappingFunction);
        Objects.requireNonNull(value);
        V oldValue = get(key);
        V newValue = (oldValue == null) ? value :
                   remappingFunction.apply(oldValue, value);
        if(newValue == null) {
            remove(key);
        } else {
            put(key, newValue);
        }
        return newValue;
    }

可以看出父类也是直接不允许value为null的

与该异常相关的注释大意就是:如果指定的键为null且此映射不支持空键或value或remappingFunction为null,则抛出NullPointerException

啊这,说了跟没说一样!

3.2、然后我们可以去查看java api文档,看看是否有做说明

emmm好吧,api文档也没给说明

只能从源码上剖析了

3.3、查看merge源码

   // Map中实现的 merge 方法
   default V merge(K key, V value,
            BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
        Objects.requireNonNull(remappingFunction);
        Objects.requireNonNull(value);
        V oldValue = get(key);
        V newValue = (oldValue == null) ? value :
                   remappingFunction.apply(oldValue, value);
        if(newValue == null) {
            remove(key);
        } else {
            put(key, newValue);
        }
        return newValue;
    }
    

    // HashMap中实现的merge方法
    @Override
    public V merge(K key, V value,
                   BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
        if (value == null)
            throw new NullPointerException();
        if (remappingFunction == null)
            throw new NullPointerException();
        int hash = hash(key);
        Node<K,V>[] tab; Node<K,V> first; int n, i;
        int binCount = 0;
        TreeNode<K,V> t = null;
        Node<K,V> old = null;
        if (size > threshold || (tab = table) == null ||
            (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((first = tab[i = (n - 1) & hash]) != null) {
            if (first instanceof TreeNode)
                old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
            else {
                Node<K,V> e = first; K k;
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k)))) {
                        old = e;
                        break;
                    }
                    ++binCount;
                } while ((e = e.next) != null);
            }
        }
        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;
        }
        if (value != null) {
            if (t != null)
                t.putTreeVal(this, tab, hash, key, value);
            else {
                tab[i] = newNode(hash, key, value, first);
                if (binCount >= TREEIFY_THRESHOLD - 1)
                    treeifyBin(tab, hash);
            }
            ++modCount;
            ++size;
            afterNodeInsertion(true);
        }
        return value;
    }

除了方法开始时对value的判空逻辑,没有直接看出其他出现value的地方如果为null会有什么影响。

这个时候我们可以回想起一开始介绍的toMap方法中的第四个参数 Supplier<M> mapSupplier 了;

这个参数很重要,目前推测就是由这个参数和写这段代码的大佬偷懒所导致空指针异常;

4、推测value不能为null的原因

还是举一开始的栗子:

    public static void main(String[] args) {
        List<Node> list = Arrays.asList(node1,node2,node3,node4);
        Map<String, String> map = list.stream().collect(Collectors.toMap(
                Node::getId,
                Node::getName,
                (n1, n2) -> n1,
                ConcurrentHashMap::new
        ));
    }

我们传入了第四个参数:ConcurrentHashMap对象;

这个时候我们可以去ConcurrentHashMap的并发场景下去理解。

4.1、ConcurrentHashMap的putVal方法和merge方法

ConcurrentHashMap的putVal方法

ConcurrentHashMap的merge方法

可以看出这两个方法的入口就已经禁止了key好value为null;

4.2、ConcurrentHashMap为什么不允许key和value为null呢

ConcurrentHashMap是如何来判断key是否存在的

可以看出,containsKey()方法的实现就是调用get()方法去获取值,若值不存在,则表示为空;

    public V get(Object key) {
        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
        int h = spread(key.hashCode());
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (e = tabAt(tab, (n - 1) & h)) != null) {
            if ((eh = e.hash) == h) {
                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
                    return e.val;
            }
            else if (eh < 0)
                return (p = e.find(h, key)) != null ? p.val : null;
            // 从该key所在的槽遍历所有节点
            while ((e = e.next) != null) {
                if (e.hash == h &&
                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
                    return e.val;
            }
        }
        // 若没有找到key,则返回null
        return null;
    }

    在并发请求下,我们在调用containsKey(key)方法前,另一线程对该key进行了put(key,null)操作,在其执行get(key)方法后返回值的value为null,在进行get(key) == null 判断时,则此时返回值还是false,是个错误的结果。故并发情况下返回null是有二义性的;

    所以在判断一个key是否存在时,若有一个key的映射是null或key就是null,则返回的值也是null,那么在上一层的

get(key) == null 就会有二义性,不知道该key是不存在导致的null,还是其值就是null,故key和value都不能为null

    而HashMap的正确使用场景下是单线程处理,则不会出现这种返回null的二义性,故可以将key和value值置null;

(ps:我有一个小疑问,如果把返回值设定value改为Node,那么就不会出现返回值是null的二义性了吧。为什么不改呢?)

5、怎么避免在使用toMap的时候抛空指针异常

5.1、若数据是从DB中查出来的对象列表

则应该从数据库中避免有null值的存在,一般在设定表的时候,都会给允许为null的字段设置一个默认值,如""

这个时候使用Collectors.toMap()方法就不会有null属性值引起空指针异常了。

5.2、若数据不是从DB从查询,而是业务逻辑中的数据,或者DB中确实有null属性值存在

  • 使用java8新特性的Optional来避免空指针

?!该不会直接禁止null就是为了推广Optional吧

        Map<String, String> map = list.stream().collect(Collectors.toMap(
                node -> Optional.ofNullable(node.getId()).orElse(""),
                // 使用 Optional 来获取值,若为null,则返回默认值""
                node -> Optional.ofNullable(node.getName()).orElse(""),
                (n1, n2) -> n1,
                ConcurrentHashMap::new
        ));
  • 直接调用map中的put()方法,而不使用toMap()中默认的merge()方法

不使用toMap()方法,直接在collect中构建map

        // 不使用toMap()方法,直接在collect中构建map
        Map<String, String> map = list.stream().collect(
                HashMap::new,
                (m,node) -> m.put(node.getId(),node.getId()),
                HashMap::putAll
        );

最后,还有个问题,为什么merge()方法中不做入参的类型判断,直接禁止value为null呢?

当构建的是HashMap对象时,为什么也禁止为null呢?

  • 11
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值