<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=koi8-r">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Hi Olexandr,<br>
<br>
Great question!<br>
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
I guess a "simple" implementation of an N-ary concat could work, but it would have performance implications (think a recursive use of Stream.concat()) but if you look at the implementation of Stream.concat's backing Spliterator, you'll see how complex it would
be to implement it in a truly N-ary way (a challenging exercise for the reader?):
<a class="OWAAutoLink" id="OWAe8fb491f-8808-e698-c277-f90f5a3810c7" href="https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/stream/Streams.java#L690">
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/stream/Streams.java#L690</a></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div class="elementToProof" id="Signature">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Cheers,<br>
–</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<b><br>
</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<b>Viktor Klang</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Software Architect, Java Platform Group<br>
Oracle</div>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Olexandr Rotan <rotanolexandr842@gmail.com><br>
<b>Sent:</b> Wednesday, 17 September 2025 15:58<br>
<b>To:</b> core-libs-dev <core-libs-dev@openjdk.org><br>
<b>Subject:</b> Stream.concat with varagrs</font>
<div> </div>
</div>
<div>
<div dir="auto">Greetings to everyone on the list.
<div dir="auto"><br>
</div>
<div dir="auto">When working on some routine tasks recently, I have encountered a, seemingly to me, strange decision in design of Stream.concat method, specifically the fact that it accepts exactly two streams. My concrete example was something along the lines
of</div>
<div dir="auto"><br>
</div>
<div dir="auto">var studentIds = ...;</div>
<div dir="auto">var teacherIds = ...;</div>
<div dir="auto">var partnerIds = ...;</div>
<div dir="auto"><br>
</div>
<div dir="auto">return Stream.concat(</div>
<div dir="auto"> studentIds.stream(),</div>
<div dir="auto"> teacherIds.stream(),</div>
<div dir="auto"> partnerIds.stream() // oops, this one doesn't work</div>
<div dir="auto">)</div>
<div dir="auto"><br>
</div>
<div dir="auto">so I had to transform concat to a rather ugly</div>
<div dir="auto">Stream.concat(</div>
<div dir="auto"> studentIds.stream(),</div>
<div dir="auto"> Stream.concat(</div>
<div dir="auto"> teacherIds.stream(),</div>
<div dir="auto"> partnerIds.stream()</div>
<div dir="auto"> )</div>
<div dir="auto">)</div>
<div dir="auto"><br>
</div>
<div dir="auto">Later on I had to add 4th stream of a single element (Stream.of), and this one became even more ugly</div>
<div dir="auto"><br>
</div>
<div dir="auto">When I first wrote third argument to concat and saw that IDE highlights it as error, I was very surprised. This design seems inconsistent not only with the whole java stdlib, but even with Stream.of static method of the same class. Is there
any particular reason why concat takes exactly to arguments?</div>
<div dir="auto"><br>
</div>
<div dir="auto">I would say that, even if just in a form of sugar method that just does reduce on array (varagrs) of streams, this would be a great quality of life improvement, but I'm sure there also may be some room for performance improvement.</div>
<div dir="auto"><br>
</div>
<div dir="auto">Of course, there are workarounds like Stream.of + flatmap, but:</div>
<div dir="auto"><br>
</div>
<div dir="auto">1. It gets messy when trying to concat streams of literal elements set (Stream.of) and streams of collections or arrays</div>
<div dir="auto">2. It certainly has significant performance overhead</div>
<div dir="auto">3. It still doesn't explain absence of varagrs overload of concat</div>
<div dir="auto"><br>
</div>
<div dir="auto">So, once again, is there any particular reason to restrict arguments list to exactly two streams? If not, I would be happy to contribute Stream.concat(Stream... streams) overload.</div>
<div dir="auto"><br>
</div>
<div dir="auto">Best regards</div>
<div dir="auto"><br>
</div>
<div dir="auto"><br>
</div>
<div dir="auto"><br>
</div>
</div>
</div>
</body>
</html>