On Sat, 19 Sep 2026 19:51:54 GMT, Rémi Forax <[email protected]> wrote:
>> src/java.base/share/classes/java/util/stream/Collectors.java line 368:
>>
>>> 366: *
>>> 367: * <p>The returned Collector requires at least one input element.
>>> 368: * It will throw {@link IllegalArgumentException} if there are no
>>> input elements.
>>
>> This behavior is very unintuitive to me. I propose to return the empty
>> string if there are no input elements, like the existing `joining` methods
>> do. In particular, I'd expect to be able to refactor existing usages of
>> `joining` by simply replacing `stream.collect(joining(delimiter))` with
>> `stream.collect(joining(listFormat))`.
>
> yes, I agree
@naotoj what do you think about formatting an empty stream? In my opinion, it's
almost never correct in practical code to use an empty string there, as the
final message will be unreadable. An empty case should always be handled
separately (probably not displaying the message at all or displaying an
entirely different message). In this sense, throwing an exception is a good
thing, as it will help to catch an unprocessed corner case. On the other hand,
the consistency argument is valid: if existing `joining(", ")` returns an empty
string, then it's probably expected to see the same from the new collector.
What do you think? This question is still valid, even if we move to
`joiningConjunctively`, etc.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/32716#discussion_r4072278281