Collectors.toMap重复问题与空值报错问题

Collectors.toMap

该方法

public Map<String, Object> toMap(List<SearchData> searchData) {
        // gson将Integer默认转换成Double
        if (searchData == null) {
            return new HashMap<>();
        }
        // return searchData.stream().collect(Collectors.toMap(SearchData::getName,
        // SearchData::getValue));
        // Map.merge check value null
        return searchData.stream()
                .collect(HashMap::new, (m, v) -> m.put(v.getName(), v.getValue()), HashMap::putAll);
    }

空值报错

使⽤Map.merge⽅法合并时,merge不允许value为null导致的

The main reason that nulls aren’t allowed in ConcurrentMaps (ConcurrentHashMaps, ConcurrentSkipListMaps) is that ambiguities that may be just barely tolerable in non-concurrent maps can’t be accommodated. The main one is that if map.get(key) returns null, you can’t detect whether the key explicitly maps to null vs the key isn’t mapped. In a non-concurrent map, you can check this via map.contains(key), but in a concurrent one, the map might have changed between calls.

key的⼆义性

Map.merge为了兼容ConcurrentHashMap还有ConcurrentSkipListMap等多线程环境下使⽤的数据结构和使⽤CAS的实现不允许 value 为 Null

key不能为null,是因为⽆法分辨是key没找到的原因所以为null,还是key值本⾝就为null.
From the author of ConcurrentHashMap himself (Doug Lea)
The main reason that nulls aren’t allowed in ConcurrentMaps (ConcurrentHashMaps, ConcurrentSkipListMaps) is that ambiguities that may be just barely tolerable in non-concurrent maps can’t be accommodated. The main one is that if map.get(key) returns null, you can’t detect whether the key explicitly maps to null vs the key isn’t mapped. In a non-concurrent map, you can check this via map.contains(key), but in a concurrent one, the map might have changed between calls.

Compare

compare 接口的意义是为了排序, 而且默认是升序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值