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

Paul Sandoz paul.sandoz at oracle.com
Fri Aug 30 02:01:08 PDT 2013


On Aug 30, 2013, at 9:01 AM, Kasper Nielsen <kasperni at gmail.com> wrote:

> Hi,
> 
> It would be nice if there was a javadoc reference from StringJoiner to
> String.join().
> 
> 
> And the example posted in StringJoiner
> 
> * <pre> {@code
> 
> * List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
> 
> * String commaSeparatedNumbers = numbers.stream()
> 
> *     .map(i -> i.toString())
> 
> *     .collect(Collectors.toStringJoiner(", ")).toString();
> 
> * }</pre>

Note latest JavaDoc is:

 * <pre> {@code
 * List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
 * String commaSeparatedNumbers = numbers.stream()
 *     .map(i -> i.toString())
 *     .collect(Collectors.joining(", "));
 * }</pre>


> Is actually "short" for String.join(", ", numbers);
> 

Not quite; List<Integer> is not compatible with Iterable<? extends CharSequence>.

    public static String join(CharSequence delimiter,
            Iterable<? extends CharSequence> elements) {

I suppose the expectation was an Iterable<Object> where toString would be called?

Paul.


More information about the lambda-dev mailing list