hg: lambda/lambda/jdk: 8023997: j.l.String.join(java.lang.CharSequence, java.lang.Iterable) sample doesn't compile and is incorrect

Henry Jen henry.jen at oracle.com
Tue Sep 3 09:22:39 PDT 2013


On Sep 3, 2013, at 7:28 AM, Kasper Nielsen <kasperni at gmail.com> wrote:

> On Fri, Aug 30, 2013 at 11:01 AM, Paul Sandoz <paul.sandoz at oracle.com>wrote:
> 
>> 
>>> Is actually "short" for String.join(", ", numbers);
>>> 
>> 
>> Not quite; List<Integer> is not compatible with Iterable<? extends
>> CharSequence>.
>> 
>> You're right,
> 
> any reason for not just taking Iterable<?> instead of Iterable<? extends
> CharSequence>. As in
> 
> public static String join(CharSequence delimiter, Iterable<?> elements) ...
> calls .toString() on each element.
> 

My understanding is for performance reason, to avoid extra toString() call.

If any transform is needed, stream.map(...).collect(Collectors.joining(…) is preferred.

As most Iterable is Collection, stream is pretty much ready to use. Otherwise, StreamSupport.stream(it.spliterator()) is available to help.

It's not as convenient, but stream can be parallel while Iterable is sequential. Also nothing prevent one from use StringJoiner directly.

Cheers,
Henry



More information about the lambda-dev mailing list