Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collectors.groupingBy doesn't accept null keys

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?

like image 492
MarcG Avatar asked Mar 25 '14 03:03

MarcG


People also ask

Can collectors toMap return null?

toMap throws a NullPointerException if one of the values is null .

Can collectors Toset return 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.


1 Answers

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()))); 
like image 66
Erling Avatar answered Sep 25 '22 09:09

Erling