Replace concat String to append in StringBuilder parameters

Pavel Rappo pavel.rappo at oracle.com
Tue Aug 19 15:00:12 UTC 2014


Brian,

Yes, this works fine in cases when 'whatever' is a j.u.Collection<? extends CharSequence> (though some may argue that it's an overkill already). However we need even more thinking and transformations in case when 'whatever' is one of these:

   * j.u.Iterator<? extends CharSequence>
   * j.l.Iterable<? extends CharSequence>
   * j.u.Enumeration<? extends CharSequence>
   * CharSequence[]

In my opinion it's not that pretty:

   * Enumeration<String> whatever = ...;
     String s = Collections.list(whatever).stream().collect(joining(", "));

   * Iterable<String> whatever = ...;
     String s = StreamSupport.stream(whatever.spliterator(), false /* or true */)
                             .collect(Collectors.joining(", "));

When we have one of these, it's even worse:

   * T[], where T is not an instance of CharSequence or T is primitive

     int[] whatever = ...;
     Arrays.stream(whatever).mapToObj(Integer::toString).collect(joining(", "));

   * Iterable<Object> whatever = ...;
     String s = StreamSupport.stream(whatever.spliterator(), false /* or true */)
                             .map(Object::toString)
                             .collect(Collectors.joining(", "));

I believe the most common case of joining objects into a string is either when objects are strings itself or they are made strings by calling Object::toString. For such a common and simple task there should be a convenient and concise one-liner.

I may be missing something.

-Pavel

On 19 Aug 2014, at 01:43, Brian Goetz <brian.goetz at oracle.com> wrote:

> What you should have written was:
> 
>  String s = whatever.stream().collect(joining(", "));




More information about the core-libs-dev mailing list