RFR 9: JDK-8040892 Incorrect message in Exception thrown by Collectors.toMap(Function, Function)
peter.levart at gmail.com
Fri Apr 25 16:13:57 UTC 2014
On 04/25/2014 02:12 PM, Paul Sandoz wrote:
> On Apr 23, 2014, at 11:06 AM, Peter Levart <peter.levart at gmail.com> wrote:
>> I propose a patch for:
> +1, nice use of putIfAbsent. Just double checking, did you run the stream unit tests?
Yes, all 60 /java/util/stream jtreg tests pass.
> I think key step was to separate map merge and accumulate functions for the toMap* methods not accepting a value merge function. Now that is the case one could still use merge, but that would be less efficient than putIfAbsent since that does not require a lambda capturing the key.
> That fix caused me to think that we should update the value merge function accepting toMap* methods for the case when the merge function returns null, as removing an entry would be most undesirable.
Yes, removing doesn't make sense. It's undeterministic. For example, if
the merge function always returns null, then duplicate keys result in
entry being absent, triplicate keys result in entry (the last one
merged) being present, quadruple keys result in entry being absent
again, etc ...
> A simple fix would be to throw an NPE if the merge function returns null, if it is acceptable to induce a side-effect of a removal, which seems reasonable given a partial result would any be returned if a null check is performed on the result of the merge function, otherwise that merge function requires wrapping e.g. mergeFunction.andThen(Objects::requireNonNull).
I think that allowing null return from mergeFunction to remove the entry
from the (maybe intermediate) map is just confusing. Throwing NPE is the
most sensible thing we can do here.
>> Here's the webrev:
>> There has already been a discussion on the lambda-dev about this bug:
>> Initially a fix has been proposed which used a private RuntimeException subclass thrown by the merger function in order to transport the values that attempted to be merged out of Map.merge() method to an outer context where they could be combined with the key to produce a friendlier "duplicate key" message. It turns out that there is an alternative approach that doesn't require exception acrobatics - using Map.putIfAbsent() instead of Map.merge(). I checked both methods in Map defaults and in HashMap implementations for possible semantic differences. Same behaviour can be achieved by requiring that the value passed to Map.putIfAbsent(key, value) is non-null and throwing NPE if it isn't.
>> Regards, Peter
More information about the core-libs-dev