collectors

Java 8 NullPointerException in Collectors.toMap

别等时光非礼了梦想. 提交于 2019-11-26 01:46:29
问题 The Java 8 Collectors.toMap throws a NullPointerException if one of the values is \'null\'. I don\'t understand this behaviour, maps can contain null pointers as value without any problems. Is there a good reason why values cannot be null for Collectors.toMap ? Also, is there a nice Java 8 way of fixing this, or should I revert to plain old for loop? An example of my problem: import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; class

Java 8 NullPointerException in Collectors.toMap

折月煮酒 提交于 2019-11-26 00:10:36
The Java 8 Collectors.toMap throws a NullPointerException if one of the values is 'null'. I don't understand this behaviour, maps can contain null pointers as value without any problems. Is there a good reason why values cannot be null for Collectors.toMap ? Also, is there a nice Java 8 way of fixing this, or should I revert to plain old for loop? An example of my problem: import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; class Answer { private int id; private Boolean answer; Answer() { } Answer(int id, Boolean answer) { this.id = id;