开发遇到stream空指针异常 .collect(Collectors.toMap())报错 NullPointerException: null at java.util.HashMap.merge错误
原先代码:
Map<String, NoticeModeEnum> eventStatusMap = new HashMap<>();
eventStatusMap.putAll(selectSystemLocations(system.getId()).stream()
.collect(Collectors.toMap(SystemEventLocation::getId,SystemEventLocation::getEventStatus));
修改后的代码:
Map<String, NoticeModeEnum> eventStatusMap = new HashMap<>();
eventStatusMap.putAll(selectSystemLocations(system.getId()).stream()
.collect(HashMap::new
,(map, systemEventLocation)->map.put(systemEventLocation.getId(),systemEventLocation.getEventStatus())
,HashMap::putAll));