collect(Collectors.toMap(k,v))报空指针异常

今天在写一个将得到的list集合中某两个属性放入到map集合中,但是一直报空指针异常

我的代码如下:

获取到的是一个对象RecordDictDetail的list集合,首先使用filter进行了过滤,将满足条件的数据筛选出来,然后将对象中的dictKey作为map集合的key,dictValue作为map集合的value,但是却发现collect那块报空指针异常,点进去源码查看原因

Map<String, String> collect = recordDictDetailList.stream().filter(e -> e.getEntNum().equals(entNum)).collect(Collectors.toMap(RecordDictDetail::getDictKey, RecordDictDetail::getDictValue));

源码:Collectors.toMap(key,value)底层调用了merge方法,对于参数进行了约束,这里的value值不允许为空,如果value值为空,他就会报空指针异常。经过查询数据库中数据,发现我的数据中存在dictValue没有赋值的情况,修改数据库中的数据,解决。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java 开发者 配置jdk1.8 一下是jdk1.8的新特性Lamdbd表达式 forEach循环遍历 List<String> names = new ArrayList(); names.forEach(String x -> { System.out.println(x); System.out.println(x); }); Map<String,String> map = new HashMap(); map.forEach((x,y) -> System.out.println(x+":"+y)); 集合里放对象 List<Book> books; books.forEach(x -> System.out.println(x.getBookName())); List<Map> books = new ArrayList(); Map map = new HashMap(); map.put("name","aaa"); map.put("price",33); books.forEach(x -> x.forEach(y,z) -> System.out.println(y+":"+z)); 方法引用 names.forEach(System.out::println); 等同于 names.forEach(x->System.out.println(x)) removeIf 条件移除 List<Book> books; books.removeIf(x -> x.getBookPrice() > 22); stream 对集合进行处理,并不会改变集合,类似于数据库的select 创建stream 集合.stream() 操作stream filter 过滤 map 转换 skip 跳过 distinct 去重 limit 取几条 结果处理 foreach 输出 collect 收集 count 总条数 List<String> names = ; names.add("zhangsan"); names.add("zhangsan"); List nNews = names.stream.distinct().collect(Collectors.toList()); Collectors.toSet() //取出名字长度大于4的,跳过第1个,取2个 List<String> nNames = names.stream.filter(x->x.length()>4).skip(1),limt(2).collect(Collectors.toList()); //把长度转换成一个集合 List<Integer> c = names.stream.map(x->x.length()).collect(Collectors.toList()); List<Integer> c = names.stream.map(String::length).collect(Collectors.toList());

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值