NullPointerException in Collectors.toMap with null entry values NullPointerException in Collectors.toMap with null entry values java java

NullPointerException in Collectors.toMap with null entry values


You can work around this known bug in OpenJDK with this:

Map<Integer, Boolean> collect = list.stream()        .collect(HashMap::new, (m,v)->m.put(v.getId(), v.getAnswer()), HashMap::putAll);

It is not that much pretty, but it works. Result:

1: true2: true3: null

(this tutorial helped me the most.)

EDIT:

Unlike Collectors.toMap, this will silently replace values if you have the same key multiple times, as @mmdemirbas pointed out in the comments. If you don't want this, look at the link in the comment.


It is not possible with the static methods of Collectors. The javadoc of toMap explains that toMap is based on Map.merge:

@param mergeFunction a merge function, used to resolve collisions between values associated with the same key, as supplied to Map#merge(Object, Object, BiFunction)}

and the javadoc of Map.merge says:

@throws NullPointerException if the specified key is null and this map does not support null keys or the value or remappingFunction isnull

You can avoid the for loop by using the forEach method of your list.

Map<Integer,  Boolean> answerMap = new HashMap<>();answerList.forEach((answer) -> answerMap.put(answer.getId(), answer.getAnswer()));

but it is not really simple than the old way:

Map<Integer, Boolean> answerMap = new HashMap<>();for (Answer answer : answerList) {    answerMap.put(answer.getId(), answer.getAnswer());}


I wrote a Collector which, unlike the default java one, does not crash when you have null values:

public static <T, K, U>        Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper,                Function<? super T, ? extends U> valueMapper) {    return Collectors.collectingAndThen(            Collectors.toList(),            list -> {                Map<K, U> result = new HashMap<>();                for (T item : list) {                    K key = keyMapper.apply(item);                    if (result.putIfAbsent(key, valueMapper.apply(item)) != null) {                        throw new IllegalStateException(String.format("Duplicate key %s", key));                    }                }                return result;            });}

Just replace your Collectors.toMap() call to a call to this function and it'll fix the problem.