In the following code, use Collectors. ToMap to translate the map of the collection. An NPE exception is found
Map<Long, Integer> wrongIdHomeworkTypeMap = questionInfoList.stream().collect(toMap(QuestionInfo::getWrongId, QuestionInfo::getHomeworkType, (v1, v2) -> v1));
Copy the code
If you look at the toMap source code, you see that the map.merge() method is called
In the merge method, value cannot be null
When using toMap, note that the value cannot be null in hashMap.
(v1, v2) -> v1 to avoid key conflicts. (In normal cognition, duplicate keys of hashMap will be overwritten, so attention should also be paid to this.)