Proposal: Replace foreach loop with Iterable.forEach in String.join(CharSequence, Iterable)

Claes Redestad claes.redestad at oracle.com
Tue Aug 17 12:20:34 UTC 2021


Hi Don,

so the win comes from forEach on the synchronized collection implicitly
being synchronized atomically, whereas foreach will synchronize on
each iterator operation and thus allow racy modification of elements
in between iteration, right?

Seems reasonable to consider, though it'd have to percolate through to 
most production deployments before you could use it - and even then
it seems somewhat fragile. And isn't this just one of many utility
methods that would have to be fixed to avoid issues with hidden
iterators completely?

String.join was recently changed to this by JDK-8265237[1]:

         for (CharSequence cs: elements) {
             if (size >= elems.length) {
                 elems = Arrays.copyOf(elems, elems.length << 1);
             }
             elems[size++] = String.valueOf(cs);
         }

Of course the body in the new code could be extracted to the same
effect. The drawback in either implementation is that it would by
necessity be a capturing lambda, so might add a small allocation
overhead. I'm biased towards avoiding performance overheads, but maybe
someone else can make a call on whether this is a worthwhile thing to
attempt.

Thanks!

/Claes

[1] https://bugs.openjdk.java.net/browse/JDK-8265237

On 2021-08-17 06:55, Donald Raab wrote:
> The following code:
> 
> for (CharSequence cs: elements) {
>      joiner.add(cs);
> }
> 
> Can be replaced with:
> 
> elements.forEach(joiner::add);
> 
> This would have the minor benefit of making it safe to use String.join with synchronized collections without requiring a client side lock. There are likely other opportunities like this in the JDK.
> 
> Also, with the addition of forEach on Iterable, and Predicate in Java 8, the following design FAQ is outdated.
> 
> https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/doc-files/coll-designfaq.html#a6 <https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/doc-files/coll-designfaq.html#a6>
> 
> Thanks,
> Don
> 


More information about the core-libs-dev mailing list