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 接口的意义是为了排序, 而且默认是升序