In Java 8, this works:
Stream<Class> stream = Stream.of(ArrayList.class); HashMap<Class, List<Class>> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass));
But this doesn't:
Stream<Class> stream = Stream.of(List.class); HashMap<Class, List<Class>> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass));
Maps allows a null key, and List.class.getSuperclass() returns null. But Collectors.groupingBy emits a NPE, at Collectors.java, line 907:
K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key");
It works if I create my own collector, with this line changed to:
K key = classifier.apply(t);
My questions are:
1) The Javadoc of Collectors.groupingBy doesn't say it shouldn't map a null key. Is this behavior necessary for some reason?
2) Is there another, easier way, to accept a null key, without having to create my own collector?
toMap throws a NullPointerException if one of the values is null .
So as long as you don't do weird things like combine function return null , the Collector always return at least a mutable container using your provided supplier function. And I think it's very counter-intuitive if an implementation would ever return null container. Show activity on this post.
I had the same kind of problem. This failed, because groupingBy performs Objects.requireNonNull on the value returned from the classifier:
Map<Long, List<ClaimEvent>> map = events.stream() .filter(event -> eventTypeIds.contains(event.getClaimEventTypeId())) .collect(groupingBy(ClaimEvent::getSubprocessId));
Using Optional, this works:
Map<Optional<Long>, List<ClaimEvent>> map = events.stream() .filter(event -> eventTypeIds.contains(event.getClaimEventTypeId())) .collect(groupingBy(event -> Optional.ofNullable(event.getSubprocessId())));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With