How to handle NullPointerException in Collectors.toMap when a mapped value is null

1 · Kumar Rohit · Oct. 29, 2020, midnight
In Java 8, Collectors.toMap throws NullPointerException when one of the mapped value is null. This is a known bug in OpenJDK. There are many workaround to handle this. The Problem The problem statement is simple, I have a list of Employee, given that employee’s salary can be null, I want to map of salary by employeeId. When we run this code, we get a null pointer exception. Solution - Using Custom Collector We are going to create a custom collector, if you have no idea how to create one, follo...